ASP.NET MVC2 Routing IIS6 - only default routes work - asp.net-mvc

I've deployed an ASP.NET MVC2 website on a Windows Server 2003 machine running IIS 6.
I'm using pretty much the default routing in a standard MVC project:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Products", action = "List", id = UrlParameter.Optional } // Parameter defaults
);
}
Navigating to http://localhost/MyApplication takes me to the List page just fine. Navigating to http://localhost/MyApplication/Products/Details/21 gives me a 404. This routing worked fine on the inbuilt development server in VS2010.
I've put in the standard IIS wildcard aspnet_isapi.dll mapping mentioned all over the place - navigating to the List page didn't work before I did - but navigating to anything other than the default route is broken.
I'd really like to keep my extensionless URLs. Does anyone have any idea as to why the routing would work for the default webpage, but no others?
*Edit: just tried adding the .aspx extension, ie now my route looks like this:
routes.MapRoute(
"Default", // Route name
"{controller}.aspx/{action}/{id}", // URL with parameters
new { controller = "Downtime", action = "List", id = UrlParameter.Optional } // Parameter defaults
);
And it has the same behaviour, except this time I get the asp 404 page instead of the html 404 page...
*Edit 2: tried it again using the following route and making sure .mvc was mapped to aspnet_isapi.dll:
routes.MapRoute(
"Default", // Route name
"{controller}.mvc/{action}/{id}", // URL with parameters
new { controller = "Downtime", action = "List", id = UrlParameter.Optional } // Parameter defaults
);
This time I got an 'Internet Explorer cannot display the webpage' 404-style page. I've now got 3 different 404 errors from using these 3 different methods...
*Edit 3 Return of the Edit: I have the site running in IIS 5.1 on Windows XP professional with only a wildcard remapping on the virtual directory, but heaven forbid I can get it to run on the webserver...

First of all, you should follow this walk-through: http://haacked.com/archive/2008/11/26/asp.net-mvc-on-iis-6-walkthrough.aspx
(just to make sure you've not missed something). This shows how to get the extension-less and extention'd versions working. You might want to at least check whether the extension'd version works to check that other things aren't misconfigured.
After that, perhaps you should try adding something like this..
http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx
... as it can really help in determining where your routes are broken.
Some other things to check:
Is IIS configured with ASP.NET? Check properties of the virtual directory. On the VDir select the configuration button and check that the usual ASP.NET extensions are mapped to the .NET 2.0 ISAPI dll. This will be something along the lines of
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll.
Is ASP.NET 2 enabled on IIS (under IIS 6 this is off by default): in IIS manager check the Web Service Extensions folder to enable it.
Have you deployed global.asax?

Found it - the routing is actually working, my URLs were not. I was using javascript to build some of the URLs and it turns out that I wasn't linking to
http: //localhost/MyApplication/Controller/Action/ID
I was actually linking to
http: //localhost/Controller/Action/ID
Building the links like this was working on the development server, but once the site was deployed to a virtual directory on the webserver those addresses are incorrect because of the extra application name in the URL.
In conclusion, be careful with your URLs - don't build them out of strings like I did.

Related

Is possible to publish web application mvc 4 (template 4.5.2) on iis7 using file system as publishing method since vs 2015?

The application in local environment runs well, but when it's published
on live web server displays
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.
I solved the problem. Yes, it is possible.
However the default page is not defined in web.config. It must be specified in RouteConfig.cs inside of App_Start directory. Something like this:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
);
As you can see, the absolute path is without cshtml extension, so could be put in url browser something like:
http://server:prort/Account/Login

ASP .NET MVC RedirectoToAction ignores the application where is located

I have a MVC application deployed in my server in a virtual directory like:
http://localhost/myapp/
Where "myapp" is the virtual directory
In my Login view, located in
"http://localhost/myapp/user/login",
I redirect to the index using RedirectToAction("Index", "Home"), it seems the application try to redirect to
"http://localhost/home/index"
instead of
"http://localhost/myapp/home/index".
The application works when it's located in the root of the IIS Web Site but doesn't work in the given situation.
It's there a way to configure the application root, that I missed?
Settings: Microsoft Visual Studio Express 2012 for Web, IIS 7 under Windows 7 , application pool ASP .NET v4.0
I am 99% positive that doing:
return RedirectToAction("Index", "Home")
is application root relative meaning that it should redirect to the application you're in regardless of the virtual directory settings or where the application is located. I mean think of the nightmare otherwise that every time you move the app to a different virtual directory you need to update the global.asax or web.config file??? Ridiculous! We have the same setup that you have and we have no issues with "app hopping."
Are you sure that RedirectToAction is causing this? Could it be that you have something like:
#Url.Content("/Home/Index")
In that case you would experience this issue and you can easily fix this by doing:
#Url.Content("~/Home/Index")
~ symbol makes it application root relative...
thats correct functionality. MVC will calculate the route as controller/action by default.
If you want this to do otherwise you need to add a route into the Global.asax:
//this is your new route which needs to be ABOVE the detault
routes.MapRoute(
// Route name
"myapp_Default",
// Url with parameters
"myapp/{controller}/{action}/{id}",
// Parameter defaults
new { action = "index", id = UrlParameter.Optional });
//This is the default route that should already be there.
routes.MapRoute(
// Route name
"Default",
// Url with parameters
"{controller}/{action}/{id}",
// Parameter defaults
new { action = "index", id = UrlParameter.Optional });
more info on routing in scott gu's blog

.net MVC 2 default Route suddenly stopped working

today the default route for my site stopped working, but the strange thing is that the global.ascx has not changed at all.
when i enter the URL mysite.com/
i get this 404 error
The resource cannot be found.
Requested URL: /Views/Start/Index.aspx
i have a bog standard default MVC route
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Index", action = "Index", id = "" }
);
the oddball thing is that even if i create a Start folder with a copy of my index view, it still doesn’t work and throws the same 404 error.
has anyone else had this issue ??
any help is most appreciated
Truegilly
Re-add ASP.Net MVC's default Default.aspx file to the site root.
This file forces requests to / to run through the routing engine.
Although not necessarily the answer to this question, when Haack's debugger is showing that your routing tables are set up fine but you're still getting 404s, check your Controller is public!

Strange route problem in ASP.NET MVC - default route not hit

I have this two routes currently in my application after decommenting out many other ones. Let me first explain that I have quite a big application already but have come to a problem where my application does not start at the root url anymore.
If I set starting page to default.aspx then webapp starts at (example) http://localhost:55421/Default.aspx. I don't want that. I want it without Default.aspx
So I went into app properties and removed Default.aspx as starting page - now it is blank field (just like in a sample new MVC app if you create it in VS 2008).
But now application does start at the required URL but issues an error:
"The incoming request does not match any route."
Also If I use route debugger it also misses all routes and catches it by catchall route.
I don't know how all of this is possible since as I said above I have two default routes configured at this time:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default",
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Pages", action = "Display", slug = "Default" }
);
Any help appreciated
Am I right in thinking you are trying to hit
http://server/{controller}/{action}/{id}
with
http://server/
If you are I think you need to provide a default for the last parameter {id}. You have a default for a parameter slug but without a default for {id} I don't think ASP.NET Routing can hit it.
If I'm right
http://server/Pages/Display
should also not hit the default route, because you are expecting id in Display?
HTH
Alex

How do you set the startup page for debugging in an ASP.NET MVC application?

How do you start debugging the application at the application root? For example: http://localhost:49742/
I'm always getting a page which doesn't exist, such as:
http://localhost:49742/Views/Home/About.aspx
Note that it would be OK to start at http://localhost:49742/Views/Home/About
Go to your project's properties and set the start page property.
Go to the project's Properties
Go to the Web tab
Select the Specific Page radio button
Type in the desired url in the Specific Page text box
While you can have a default page in the MVC project, the more conventional implementation for a default view would be to use a default controller, implememented in the global.asax, through the 'RegisterRoutes(...)' method. For instance if you wanted your Public\Home controller to be your default route/view, the code would be:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Public", action = "Home", id = UrlParameter.Optional } // Parameter defaults
);
}
For this to be functional, you are required to have have a set Start Page in the project.
This works for me under Specific Page for MVC:
/Home/Index
Update: Currently, I just use a forward slash in the "Specific Page" textbox, and it takes me to the home page as defined in the routing:
/
Selecting a specific page from Project properties does not solve my problem.
In MVC 4 open App_Start/RouteConfig.cs
For example, if you want to change startup page to Login:
routes.MapRoute(
"Default", // Route name
"", // URL with parameters
new { controller = "Account", action = "Login"} // Parameter defaults
);
If you want to start at the "application root" as you describe right click on the top level Default.aspx page and choose set as start page. Hit F5 and you're done.
If you want to start at a different controller action see Mark's answer.
Revisiting this page and I have more information to share with others.
Debugging environment (using Visual Studio)
1a) Stephen Walter's link to set the startup page on MVC using the project properties is only applicable when you are debugging your MVC application.
1b) Right mouse click on the .aspx page in Solution Explorer and select the "Set As Start Page" behaves the same.
Note: in both the above cases, the startup page setting is only recognised by your Visual Studio Development Server. It is not recognised by your deployed server.
Deployed environment
2a) To set the startup page, assuming that you have not change any of the default routings, change the content of /Views/Home/Index.aspx to do a "Server.Transfer" or a "Response.Redirect" to your desired page.
2b) Change your default routing in your global.asax.cs to your desired page.
Are there any other options that the readers are aware of? Which of the above (including your own option) would be your preferred solution (and please share with us why)?

Resources