C# MVC Debug which controller returned view - c#

So I just started at a new company and I'm trying to understand their code and it is quite complex. I am trying to debug a page and I know which view it is and I can set a break point in the view but I can't figure out which controller returned this view. How can I do this?
Recap: I can break in a view and I need to figure out what controller it came from.

If the application is using the Razor view engine (.cshtml files) you can use the following inside your view/layout to display the controller and action names:
#ViewContext.RouteData.Values["controller"].ToString()
#ViewContext.RouteData.Values["action"].ToString()
If it is using the WebForms view engine (.aspx files) you can do something similar with:
<%= RouteData.Values["controller"]%>
<%= RouteData.Values["action"]%>
The controller class will usually be named as in the route data plus "Controller". The action names will usually match a method name in the controller.
You may also consider getting a branch of the project just for you, and then install glimpse via Nuget. That may help you understand better the application.

Views usually are named after Controller action names. And views are placed in folders named after controllers.
Check this one for folder structure: http://www.codeproject.com/Articles/492833/ASP-NET-MVC-4-Part-2-Project-Items
Also you can set breakpoints in controllers and see which one is hit.
And I recommend watching free pluralsight course on MVC. I helped me a lot when I started with MVC.

Related

Go To Controller shows "Unable to find a matching controller." error

In VS 2015, for my Web.API project, I installed MVC 5 from nuget via npm.
This installation added references and generated web.config file under Views folder.
I added new Controller with generated CRUD functions. When I right click and go to View on action it goes with no problem. But reverse action does not work. If I navigate to view via browser it works as well. Web app compiles as well.
Is there any way for me to fix this navigation problem in VS? Did I forget to add something?
Following Works
Following gives Error:
P.S: If I create brand new sample MVC 5 app both actions work as expected, this only happens Web.API project where MVC 5 added later on via nuget.
This happened to me once. Delete the view and try adding the view again by right clicking on the controller action method and clicking Add View again. I think it would solve the problem
It happens while the controller or view may be not exactly in the controller folder or view folder respectively. It may contain an additional folder. You can solve this issue through adding a new view and delete the old view.
There appears to be a default shortcut key Ctrl-M then Ctrl-G.
This will automatically switch between the controller and view page. If you're on the controller/action it will switch to the view and vice versa.
My situation is not exactly the same as your (I added a new controller and new view), but I was receiving the same error message. Ensure that the name of your controller matches the name of the folder that the view is in.
For example, if the controller is TodoController.cs ensure that the view is in a folder called Todo. It doesn't seem to matter what the view .cshtml file is called. That's what fixed it for me.
I had this exact scenario when my controller file name did not match the controller class name:
I had a controller class named InvoicesController, but the file name was InvoiceController.cs. After renaming the file name to the class name of the controller the switching back from the view to the controller started working
I just had this and the controller was in the same folder as the Controllers folder. I moved it into the folder and it worked after that.
At least reading this, I found the shortcut key to switch between controller and view :-)

Redirect from one Razor View to another Razor View

I am new to Razor Views and I am working on Xamarin studio, making a hybrid application that works only on WebView.
Long story short, I am not working with MVC. No models and no controllers here, only Razor Views. Now this might be a wrong question, but is it possible to navigate from one razor view to another??
Like, from one .cshtml to another .cshtml, without going through any Controller, because I don't have Controllers.
Any .NET helper like, #Html.ActionLink("Text", "someFolder/page2.cshtml");
Update 1
Screenshots of index.cshtml and MainActivity.cs, from which I am generating HTML for index:
The breakpoint is on the line that is generating the HTML.
No you cannot redirect to another view without calling action.
In asp.net mvc you have actions and against actions View is returned, we can show two views in a view without calling action but cannot redirect to some view without calling action.
If you don't need controllers then you are not following mvc architecture so then i would prefer to use simple html pages, there is no need to use mvc in this case.
you can use PartialView as shown below but it will just use one view in another view it does not redirect to another view as you have mentioned :-
#Html.Partial("~/YourPath", Model);
For more information :- http://www.codeproject.com/Tips/617361/Partial-View-in-ASP-NET-MVC

How to set up locations where view engine should find view in ASP .NET MVC3 or how to render view from another MVC application?

For example I have two similar simple MVC applications.
Application1:
HomeController -> Index action which returns AnotherHome view of
Application2:
AnotherHomeController -> AnotherIndex action which also returns AnotherHome view
Application1 Index action:
public ActionResult Index()
{
return View("AnotherHome");
}
I need to run Application1 and render AnotherHome. When I run app it is obvious that I get the following error:
The view 'AnotherHome' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Home/AnotherHome.aspx
~/Views/Home/AnotherHome.ascx
~/Views/Shared/AnotherHome.aspx
~/Views/Shared/AnotherHome.ascx
~/Views/Home/AnotherHome.cshtml
~/Views/Home/AnotherHome.vbhtml
~/Views/Shared/AnotherHome.cshtml
~/Views/Shared/AnotherHome.vbhtml
How can I force view engine to search view, for example, in Application2/Views/AnotherHome/AnotherIndex.cshtml ???
The short answer is that you can't render a view that's in a different app's application directory. This is a security issue in IIS, and it won't let you access files outside of its application path.
A longer answer is that you could map a virtual directory in IIS into the app directory somewhere. But this would require specific IIS configuration to manage this, and it's something that can easily break if someone else is maintaining things.
It would probably be better to keep separate copies of the view anyways. What happens if you modify it for one app, and then it breaks the other?
You will have to override the ASP.NET default view engine and override the view paths, however you will have to configure somewhere the location of the second application, unless you want to hardcode it. Refer to this question on how to change the view engine search paths, How to change default view location scheme in ASP.NET MVC?.
To share views between apps, compile the views into a dll (Google for razor generator) and share the dll

Where should I write logic to get menu from database?

I am using Asp.Net MVC 3.0 and I have my _layout.cshtml in Shared folder. Problem is I want to generate menus in this .cshtml which should be loaded from database. But as I understand _layout.cshtml won't have any action etc associated where I can write logic and I don't want to write all this code in cshtml itself. Are there any options to write logic for cshtml within Shared folder?
You can setup a Controller and a view to render the menu and call it inside the _layout.cshtml.
#{ Html.RenderAction("Index", "Menus"); }
Eranga is correct, but let me expand on his answer, to answer your question specifically.
What you can do is create a new controller ("menus" for example), and create an action called default. Have this action return a view, calling it whatever you would like. Now go to your shared folder and add the view using the name you just specified.
Now for the cool part. By default, the MVC framework will look in the controllername/viewname path first, and if it fails it will then look at your shared/viewname path, which is where the view you just created resides! Neat, huh? ;p
Check out http://www.aspnetmvcninja.com/views/view-search-paths for more info on MVC search paths.
#Eranga has given you a good head start on implementing the feature you requested. I think the below two articles will be helpful as well:
Html.RenderAction and Html.Action:
http://haacked.com/archive/2009/11/18/aspnetmvc2-render-action.aspx
A sample implementation of Html.Action method with caching:
http://www.tugberkugurlu.com/archive/donut-hole-caching-in-asp-net-mvc-by-using-child-actions-and-outputcacheattribute

ASP.NET MVC Custom Routing in Sub Folder

I have searched on Google (may be with wrong keyword) and visited the tutorials of ASP.NET/MVC website. But didn't understand the routing of MVC properly.
I have a scenario of the following figure. I want to display the Index.cshtml when the website lunched.
I have changed the RegisterRoutes method of Global.asax.cs file in various ways. The latest code (not working) is below
routes.MapRoute(
"App", // Route name
"App/{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
I will be very happy if someone explains the MapRoute method with different example along with answering my problem.
I don't think the problem is with your routes, I believe it is caused by moving your View and Controller folders to the App folder.
MVC use "convention over configuration" for stuff like where the folders are located, I'm guessing it can't find the Views/Controllers in the new folders?
From here:
These folders are included even in an
Empty ASP.NET MVC application because
the ASP.NET MVC framework by default
uses a “convention over configuration”
approach and makes some default
assumptions based on folder naming
conventions. For instance, controllers
look for views in the Views folder by
default without you having to
explicitly specify this in your code.
Sticking with the default conventions
reduces the amount of code you need to
write, and can also make it easier for
other developers to understand your
project.
I'm not saying that it's not possible to have a folder structure like you have, I just dont believe its supported out of the box.
Personally I'd recommend moving back to the default, if you need to keep this structure then I think you may need to implement your own ControllerFactory and ViewEngine (I'm not sure if these are the correct hooks that you would need to change).
Edit:
Are you trying to implement App as an Area?
Edit2:
If you are trying to add App as an area there are some steps you will need to follow, The reason I wasn't sure if this was what you were trying to do is because I thought areas had to exist within an Areas folder.
There are some steps outlines here Including how to register your area if that is what you are trying to do.
ASP.NET MVC 3 has a built in feature for sub folders. That is called Areas (thanks #Morten for let me know the word). But after finishing the MSDN Walkthrough for Areas, I was still unable to run the web application. The error was showing Configuration errors. #Danny also noticed that.
Then I searched and found that, the Web.config files in the sub folders shouldn't contain application specific properties.So, I removed those properties from the Web.config files. Then I was able to run. But there were some runtime errors when I wanted to navigate to my sub folder/area views. The error was "Could not load type ‘System.Web.Mvc.ViewPage<..<>’" when I click on the links for the views of the Areas folder.
Then I again searched and found helpful this post. And then my application successfully run and I can navigate all of the pages.

Categories

Resources