This is a very strange problem. I am new to the mvc world coming from web forms and i am trying to understand its concepts. Using the MVC template in vs 2013 (premium), I have built a project. In order to see how things work:
I create a new controller 'IndexController' and put it in the folder .../Controllers/IndexController.cs
I create a new View for this controller 'Index.cshtml' and put it in the corresponding folder: .../Views/Index/[#] Index.cshtml
Then I change the routing so that the default routing will now point to this IndexController and not to the default HomeController
Here is my routing table:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Index", action = "GetIndexPage", id = UrlParameter.Optional }
);
}
}
You can see that I am using 'GetIndexPage' as the default action instead of 'Index' (I'm playing around and see how it works)
Whenerver I make a change in the Index.cshtml (say I add a simple markup and hit 'Run' I always receive the error message
Server Error in '/' Application. The resource cannot be found.
Looking at the address bar I see that the browser is looking for the resource 'localhost:xxx/Index/Index' instead of 'localhost:xxx/Index/GetIndexPage'. To solve this problem, I go in the IndexController and put a breakpoint inthe line
return View(...);
Now I hit 'Run', after stopping at the breakpoint, every thing works perfectly. So it is not a problem of routing since the page is displayed after this breakpoint trick. Visual Studio seems to mess up with the deployment to the IIS Express I am using after I have made a change to the cshtml view. The problem does not occur when I make a change in the code behind of the controller. I don't know where to look at...I have spent this whole night trying to find a solution in Google and stackoverflow...I don't want to reinstall the whole visual studio. Any help, any hint to a certain direction will be greatly appreciated.
After a lot of trials I discover that mvc framework wants the action and the view (that this action returns) must have the same name. In my case this is what has worked:
route configuration:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Index", action = "IndexPage", id = UrlParameter.Optional }
);
IndexController:
public class IndexController : Controller
{
public ActionResult IndexPage()
{
return View();
}
}
You can see that route action has same name with method of controller: IndexPage
And finally create a view with name IndexPage.cshtml
With this configuration when I make a change in the cshtml file it is immediately reflected in the browser and there is not the error reported above.
I just want to have a confirmation if this is indeed the way things must be set up with the mvc approach. (thanks with your help)
Related
There is an application on asp.net mvc. And such an interesting moment. There is a start page which is the default configuration in the route. And when you start the application, it writes only localhost ... How to ensure that the default page prescribe its full path like everyone else, i.e. localhost / Controller / Action.
Tell me how to achieve this? I nned like:
How can I achieve this?
My RouteConfig:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Authorization", action = "Index", id = UrlParameter.Optional }
);
}
The entire point of the "default" route is having something to show the user when no route is filled in. People that will visit your site won't type in or find "www.yoursite.com/authorization/index" so that's why the routing configures a default route.
If you want your page to show something different by default you need to change the data in the default route in your routes.config and create a controller for it. You could also let the default land at an action that only redirects to "authorization/index" which will change the URL.
Lastly you could simply fix it with some javascript on the page, but I think this will also cause a page reload so fixing it server side is probably best.
I've been working on this problem for a while now and I'm not sure what else to try. The error message I'm getting is this:
System.InvalidOperationException: The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Home/Index.aspx
~/Views/Home/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Views/Home/Index.cshtml
~/Views/Home/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml
The view is located at Views/Home/Index.cshtml
Here's the RouteConfig class
public class RouteConfig {
public static void RegisterRoutes(RouteCollection routes) {
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
Even putting a breakpoint in the Index action on the Home controller doesn't trigger. The Index controller action just returns the view:
public ActionResult Index() {
return View("~/Views/Home/Index.cshtml");
}
The project settings are set to Use Local IIS Web server
Building the project succeeds. There are no build events. Target framework is .NET Framework 4.5. Output type is Class Library.
I was comparing everything I could think of with another project that is working and there's nothing standing out to me. I even compared the .csproj files of both and the only differences seemed to be <Content Include lines for files that are not in the other project. Any ideas on where to look next to fix this?
You should just say return View("Index"); or just return View(); It would search for a view with the same name as of action method
Try right clicking on your index.cshtml and select Properties. In the properties window set the Build Action to Content. This may solve your problem.
Also verify you have the _Layout.cshtml file in the shared folder if your _ViewStart.cshtml points to that.
After deciding to stop focusing on the error and start focusing on why breakpoints were not being hit, I found myself at this answer
Ok so after 4 hours wasted, I find that setting my web project as the startup project solves the issue! This surely must be a bug...
I hope I save someone out there half a day :)
Turns out the website project was not set as the startup project. Unfortunately, other answerer, I still ended up spending way to long figuring that out.
I don't know what happened to my website. From today the default action "Index" in only one controller doesn't work anymore.
If I call http://website.com/Valuation i get 403 error page because the webserver doesn't route my request and try to browse the folder. If I write http://website.com/Valuation/Index everything works.
I search in all the code but i can't find the problem, everything seems fine like other controllers.
How can i find the problem? Do you know if there are a known issue that cause that problem or you know if there is a trace\log\debugger of routing requests?
Thanks
Mic
Most probably the issue is you have a folder by name Valuation in your website root. Thats why the valuation index action is not working. Instead of routing to the Controller Action the url http://website.com/Valuation is routing to the Folder Valuation.
Delete this folder Valuation from your root or rename it then this url http://website.com/Valuation will work.
Also check if the ValuationController has the public ActionResult Index() ([HttpGet] method.
Check ~\App_Start\RouteConfig.cs file
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional}
);
}
}
Strange problem I got here. I using Visual Studio 2012. When I start debugging my web application from .cshtml tabs I getting this error
Server Error in '/' Application. The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its
dependencies) could have been removed, had its name changed, or is
temporarily unavailable. Please review the following URL and make sure
that it is spelled correctly.
Requested URL: /Views/Header/GeneralInputs.cshtml
Version Information: Microsoft .NET Framework Version:4.0.30319;
ASP.NET Version:4.0.30319.225
When I run it from .cs tabs everything is fine and runs well. Whats wrong?
Here's my RouteConfig
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Header", action = "GeneralInputs", id = UrlParameter.Optional }
);
}
}
Also I tried to adding this line into the WebConfig, but that didn't help
<modules runAllManagedModulesForAllRequests="true" />
UPD:
what I got in
HeaderController
public ActionResult GeneralInputs()
{
return View();
}
I think you can't request this page like this:Requested URL/Views/Header/GeneralInputs.cshtml. You should request Header/GeneralInputs. You must request the Action in the controller. No the view in the controller
The feature you are seeing can actually be quite useful: directly run the page (Controller/Action) your cursor is in.
But when you frequently call this from a View, or from Actions that require parameters, you can fix your starting point from Project|Properties|Web, and then check the (o) Specific Page option.
The action name "GeneralInputs" should be in your HeaderController and GeneralInputs.cshtml file should be in Header View folder. if both are well and good. Then don't run your application while the cursor in GeneralInputs.cshtml page. Keep the cursor in the controller then run your application.
I have an mvc 2 application that is running in ii6 in the test environment and production server.
The test enviroment runs just fine, but after moving to production all pages except the home page server up 404 errors.
I have followed step 2, here: http://blog.codeville.net/2008/07/04/options-for-deploying-aspnet-mvc-to-iis-6/ and added a .aspx extension to the route, and have tried the wildcard mapping. It doesn't appear to make any difference.
I put the diagnostic file found here: http://bradwilson.typepad.com/blog/2010/03/diagnosing-aspnet-mvc-problems.html into the directory and loaded it, but it does not report any errors or problems.
I even wiped the test server and reinstalled the app from scractch, setup the wildcard mapping and it worked fine.
Right now the pages are being routed like: Home.aspx/About and my routing table looks like this:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{*favicon}", new { favicon = #"(.*/)?favicon.ico(/.*)?" });
routes.MapRoute(
"Default", // Route name
"{controller}.aspx/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"NewEmployee",
"{controller}.aspx/{action}",
new { controller = "NewEmployee", action = "Index" }
);
routes.MapRoute(
"Admin",
"{controller}.aspx/{action}",
new { controller = "Admin", action = "Index" });
routes.MapRoute(
"AccessMaster",
"{controller}.aspx/{action}/{id}/{subid}",
new { controller = "AccessMaster", action = "Index", id=UrlParameter.Optional,subid=UrlParameter.Optional });
routes.MapRoute(
"Root",
"",
new { controller = "Home", action = "Index", id = "" }
);
}
I'm at a loss here. Am I doing something wrong? Is there something wrong with the server?
Try registering ASP.NET with IIS using the following command:
aspnet_regiis /i
Also make sure that you have enabled the correct version of ASP.NET in IIS (Web Service Extensions folder):
You also have many unnecessary routes routes. For example the NewEmployee and Admin routes are totally equivalent meaning that only the first route in this list will ever be matched. But that's another problem, it is unrelated to the deployment errors you are getting. You could fix your routes once you make your application successfully run.
It looks like deleting and recreating the virtual directory magically fixed whatever was wrong with it.