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

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 :-)

Related

How to Map Two Project in Single Solution and How to Redirect to One project action to second Project action

I have use two Project under single solution --
PayrollMVC is Name of Solution which contains two Project
1) Payroll
2) Employee
when i Login from Payroll and Redirect to Employee Project Controller action than it does not Call Controller Action and Layout of Employee Project ..Please
Suggest me How to Map Two project and how to Call Action from one Project to Another and
also I Please Tell me What is the issue of not Calling Action and Layout of Employee Project
One solution to call an action from one solution to the other would be to use Server Transfer in the controller like so:
public ActionResult Whatever()
{
string url = //...
Request.RequestContext.HttpContext.Server.TransferRequest(url);
return Content("success");//Doesn't actually get returned
}
Copied from https://stackoverflow.com/a/20913290/201648
This is useful if you want to quickly transfer a handful of controllers to a page on another website using the same URL, e.g. http://example.com/payroll/foo could show the content http://example.com/employee/foo but still appear at http://example.com/payroll/foo. This solution is quick to implement but also hacky, and it doesn't solve you layout issues (please continue reading about areas below for a complete solution).
In terms of not calling the actions and layout, it's because they're in a different project (so the current project doesn't know about them). For this reason you'd probably want to go to the trouble of setting up areas. There is a full guide for areas here:
https://msdn.microsoft.com/en-us/library/ee671793%28v=vs.100%29.aspx
You can setup shared layouts for areas as described in this link:
http://brockallen.com/2012/08/31/sharing-a-single-_viewstart-across-areas-in-asp-net-mvc/
All the routes (for the controllers) can be configured centrally. I'd start by looking at Registering Area Routes and Linking Between Areas from the first link I posted.

C# MVC Debug which controller returned view

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.

Naming a view "Properties"

This is a .NET MVC 4 website using index for default. So far everything has been fine for this site till I added a new view folder named "Properties", added an index view and created the controller plus a link on the nav bar for it. Everything compiles nicely.
When I run debug, and use the link, I get a 403.14 error trying to browse the directory. If I type the full route with the index it goes to the page correctly. All link and code is as it should be. I have not created any kind of model or form for this page yet.
When I rename the View and controllers to simply "Property" everything works correctly.
Is "Properties" as a keyword to avoid?
Was I asleep that day in class?
I'm assuming you're running into this problem during local development? There's a physical directory called Properties in your project, which contains the AssemblyInfo file. When you're attempting to browse to /Properties, it would first look for a physical location, which it finds, then fail because it can't find a document in it.
If you publish this to a real web location, there won't be a Properties directory, so it should work. Or you can simply delete the directory and then it should work locally.
But personally, I wouldn't recommend that - just pick a different name. It's easier to just try to avoid conflicts like this whenever possible.

Forms app converting to adding Razor (v4) MVC doesn't look in Shared folder for layout

I am converting an Forms based application to add some MVC 4 View Pages.
I have been able to get pages to work but when I create a view, it never looks in my Views/Shared folder and requires me to put the shared page inside the same view folder instead of defaulting to Shared layout if not specified.
Where is the routing information precedence in MVC4 stored so I configure it to look in the shared folder first? In a slightly related question, is there any way to get the Add Contoller wizard option to show up when right clicking on the Controllers folder and the Wizard to for the scaffolding views? (See picture below of add wizard for example)
Update: After some frustrating time I have added the file into the path desired by the error message and I still getting the error message. It seems the routing is working because when I return a string, the route finds and displays it. The problem appears to happen when I use an ActionResult and for some reason cannot find the "associated" files.
In your _ViewStart.cshtml file in your Views folder, you should have something like:
#{
Layout = "~/Views/Shared/_Layout.cshtml";
}
That's what tells it where to look for the layout.

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

Categories

Resources