ASP.NET MVC not serving default document - asp.net-mvc

I have an ASP.NET MVC application where the default page should be index.html which is an actual file on disk.
I can browse to the file using www.mydomain.com/index.html so I know it will be served and exists but if I use www.mydomain.com I get a 404.
I have ensured that the default document is correctly set in IIS7 and I have even gone so far as to commented out all the routes defined in my global.asax to ensure I don't have a route causing this problem.
So to summarize:
I have an index.html file on disk and IIS7 is set to use index.html as the default document.
If I remove my ASP.NET MVC application and leave the index.html file is served as the default document as expected.
If I publish my ASP.NET MVC application then the index.html doesn't get served by default document.
Does anyone know how to get ASP.NET MVC to serve the default document?

ASP.Net MVC routing is controlled by the Global.asax / Global.asax.cs files. The out-of-the-box routing looks like this:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
When no path is specified, ie. www.domain.tld/, the route is the empty string, "". Therefore the routing looks for a controller with that name. In other words, it looks for a controller with no name at all. When it finds no such controller it serves up a 404 NOT FOUND error page.
To solve the problem, either map that path to something meaningful or else ignore that route entirely, passing control over to the index.html file:
routes.IgnoreRoute("");

I had a similar problem with a WebForms application. In your web.config make sure the resourceType attribute of the StaticFile handler under system.webServer is set to Either.
<add name="StaticFile" path="*" verb="*" type="" modules="StaticFileModule,DefaultDocumentModule,DirectoryListingModule" scriptProcessor="" resourceType="Either" ...

I found a way around this. If you want index.html to be in the root of your MVC application (i.e next to your controller/model/view/appdata etc folders), you can do this:
Say you have home.html, aboutus.html and contactus.html.
//this route matches (http://mydomain.com/somekindofstring)
routes.MapRoute(
"SingleRootArg",
"{id}",
new { controller = "Home", action = "Details", id=""});
// so now that you have your route for all those random strings.
// I had to do this for vanity urls. mydomain.com/ronburgandy etc. however,
// mydomain.com/index.html will also come in just how you'd expect.
//Just an example of what you COULD do. the user would see it as root html.
Public ActionResult Details(string id)
{
//let me save you the trouble - favicon really comes in as a string *ANGER*
if(String.IsNullOrEmpty(id) || id.ToLower().Contains("favicon.ico"))
return Redirect("~/index.html");
if(id == "aboutus.html")
return Redirect("~/aboutus.html");
if(id == "contactus.html")
return Redirect("~/contactus.html");
if(id == "index.html")
return Redirect("~/index.html");
}
index.html aboutus.html index.html are now at the same level as my CSPROJ file.

Sorry for resurrecting this mummy, but i don't believe this issue was ever a default document issue. In fact you probably don't want to have a default document set as many of the other answerers have stated.
Had this problem as well, a similar problem. the cause of my issue was that the Application Pool for the site was set to use .NET Framework v2 and should have been set to v4. once I changed that it loaded correctly.

You could ignore the route in your MVC application and let IIS serve it.
routes.IgnoreRoute("index.html")
etc

I suspect you added index.html yourself as that extension would be unknown to the mvc framework.
Your default index page in mvc is //domain/home/index and is physically index.aspx.
If you call your domain using //domain then the routing engine will assume /home/index.aspx and not index.html.

Related

How to create an MVC route to intercept classic ASP URLs?

My question up front
How do I construct a route so that MVC will intercept the classic ASP URL and instead execute an MVC action?
I am migrating a legacy classic ASP application to MVC, and need to have MVC intercept a couple of the legacy ASP URLs because they are major endpoints for external access to the application. But I can't seem to figure out how to do it correctly.
I checked a few other questions and didn't quite find what I'm looking for, but maybe my search-fu is poor today. This one is specific to areas but looks similar to mine which still doesn't work, and this one is a possible workaround but I'd really rather handle this completely within MVC and eliminate the legacy file completely.
What I want to do
Given: /foo/bar.asp
Map to: /InboundLinks/HandleBar
(one URL will be a GET request, but the other will be a POST with some sensitive data, so I need them to be intercepted and the POST data still available to MVC, not sure if a 301 redirect will do that or not)
What I DON'T want to do
I do NOT want to run the classic ASP pages at all. (I'm willing to have it solely do a 301 redirect to the MVC URL if that is the only workaround, but that's it) I want the URLs to be intercepted and handled by MVC. I say this because a few questions I found here and elsewhere seemed to generate some confusion on that point.
What I've already tried
routes.MapRoute(
name: "LegacyBarUrl",
url: "foo/bar.asp",
defaults: new { controller = "InboundLinks", action = "HandleBar" }
);
But this returns a 404 Not Found error.
Environment
Visual Studio 2013 running in local dev mode on Windows 7. Deployment will be to IIS 7 on a locked down server I don't control, so installing HTTP modules on the server isn't an option unfortunately. The domain will remain the same.
Many thanks in advance for any help/guidance/etc.
What you've tried must work. Make sure it comes at the top of your routing configuration, and the default route comes after it.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "LegacyBarUrl",
url: "foo/bar.asp",
defaults: new { controller = "InboundLinks", action = "HandleBar" }
namespaces: new[] { "YourProject.Controllers" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "YourProject.Controllers" }
);
}
EDIT:
OK, I've tried this myself as well and it really does not work. So you have two options:
(1) capture and route your request at the IIS level: If you take this path, this extension might be very helpful: http://www.iis.net/downloads/microsoft/url-rewrite]
(2) write your own RouteBase and redirect legacy routes before MVC looks up the routing table: If you take this path, this article would be very helpful to you (it would be too long to write the code here): http://www.mikesdotnetting.com/article/108/handling-legacy-urls-with-asp-net-mvc
To Anyone running into this problem, the 404 is because the server is looking for the physical file before getting into the routes. What is needed is a handler for the extension, in this case for the classic asp that is going to catch the request so the server doesn't look for the file anymore, and the request is handled by your handler.
Add an entry to the web config file in the handlers section like this:
<add name="ClassicASPHandler" path="*.asp" verb="GET" type="System.Web.Mvc.MvcHttpHandler, System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
Then add the needed route to the configuration routes table. Now, the route will be handled correctly as opposed of looking for the file and returning a 404.
routes.MapRoute(
name: "LegacyBarUrl",
url: "foo/bar.asp",
defaults: new { controller = "InboundLinks", action = "HandleBar" }
namespaces: new[] { "YourProject.Controllers" }
);

ASP.NET MVC 404 Error for SubDirectories

I've added a custom 404 page to my asp.net mvc application. It works totally great except for on some paths that have been excluded from the MVC routing engine. As you can imagine, I'd like my 404 page to work for those URLs as well.
So the question is: Can I add some setting in IIS I can use to just point it to the 404 page's endpoint.
Thanks!
No, there isn't any custom setting on IIS.
Works for me. I have the following settings set in web.confg
My RegisterRoutes method is as below
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
I ended up writing a StaticContentController that simply opens a file stream and returns it to the browser. The path is controlled tightly by the routing engine to avoid security issues, and it is heavily cached.
A note on the file stream, when I first implemented it, I was using MVC's return File(... method, but that locks the file while it is streaming it, so I had some instances of file contention for files that were written to and served concurrently. That's why I switched to returning a FileStream, so I could pass the correct file sharing options that allow for the file to be written to by another process.

Rewrite old .aspx path to new MVC path

i have a mixed aspx/MVC webapp project and need to rewrite incoming URL's either in the MVC routing or through IIS rewriting. whatever works. I cannot figure this out.
I have the following OLD path:
/Article/Nugget/Article.aspx?articleId=30
and i need to rewrite this to:
/Article/Nugget/30
The issue is the MVC route is reading in the Article.aspx being passed as a parameter and anything i do to rewrite this in IIS7 is being ignored. Well.. the issue is i don't have a clue :)
Try something like:
routes.MapRoute(
"Article",
"Article.aspx",
new { controller = "Article", action = "Nugget"}
);
With a parameter named articleId in your action method of course
public ActionResult Nugget(int articleId)
{
..
}

ASP.NET MVC: CSS file returning a 302 error when it exists

I'm getting a 302 error returning on a single CSS file on an ASP.NET MVC 2 site in localhost this morning and I don't know what would have changed to cause this.
The localhost site uses IIS 7.5, though I've had limited experience with IIS so I haven't looked to much in to what could be going on there.
The URL to the CSS file is:
http://localhost/MySite/Content/Site.css?v=16
and the location header on the response looks like this:
/MySite/Account/Login?ReturnUrl=%MySite%2fContent%2fSite.css%3fv%3d16&v=16
This makes me think that MVC is redirecting the static file or something like that, however if that was the case, then I would expect all my images, CSS and JavaScript files to be doing the same which they're not. Just in case, here is a simplified version of RegisterRoutes() in Global.ascx:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("", "Account/{action}/", new { controller = "Account" });
routes.MapRoute("", "{action}", new { controller = "Home", action = "Index" });
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Error",
"{*url}",
new { controller = "Home", action = "ResourceNotFound" }
);
}
Also, if I change the name of my CSS file to Site2.css and reference that instead, the same thing happens.
What's going on?
The redirect to the logon method makes it look like this is because of permissions on the directory or the file rather than an MVC route catching it. (If it were caught by an MVC route, it would probably rather result in an error determining which controller and/or action to use.)
ASP.NET MVC itself leaves static files alone, but if ASP.NET in general decides that the anonymous user doesn't have access to the CSS file or its directory, ASP.NET will redirect to the log-on URL, which will be an ASP.NET MVC action.
Looks like the authorization rules in the web.config are saying that you have to be authenticated to see the css pages. You should be able to prove that by logging in and seeing if you can get the css file to be served correctly.
I'd add a location section to the web.config to remove the authorization requirement on the content directory. Taken from http://support.microsoft.com/kb/316871
<!-- This section gives the unauthenticated user access to all of the files that are stored in the Content folder. -->
<location path="content">
<system.web>
<authorization>
<allow users ="*" />
</authorization>
</system.web>
</location>

Override directory listing with MVC URL routing

Recently, I partially converted an Asp.Net web forms application to use MVC. We still have parts of the application in web forms (.aspx pages) and use MVC routing to work with Controllers and such.
I added an MVC route like
routes.MapRoute("Users", "Users/{controller}/{action}/", new { controller = "Timesheet", action = "List" });
There is a folder called "Users" which contain a few aspx pages we still use.
When I hit the URL http://localhost/Users/ I get a directory listing of the contents of the "Users" folder. Apparently, the directory listing takes precedence over MVC url routing and this might be overridden by modifying the IIS7 server settings.
How could I override this behavior, via code or web.config changes?
References:
http://forums.asp.net/t/1251156.aspx/1
http://learn.iis.net/page.aspx/121/iis-7-and-above-modules-overview/
Setting RouteExistingFiles=true on the RouteCollection achieves just that. It will allow ASP.NET MVC to handle routes even for existing directories.
Use this ignoreroute:
routes.IgnoreRoute("{WebPage}.aspx/{*pathInfo}");
Listing the RegisterRoutes method
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{WebPage}.aspx/{*pathInfo}");
//routes.MapPageRoute("users", "users", "~/admin/default.aspx");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "home", action = "index", id = UrlParameter.Optional } // Parameter defaults
);
}
This would exclude all pages whose extension is ".aspx" from routing.

Resources