Override directory listing with MVC URL routing - asp.net-mvc

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.

Related

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.

IIS 7.5 hosting mvc 3 razor app default page

I have a MVC 3 Razor app and I need to deploy it on http://www.mydomain.com.
The trouble is that that, when I hit http://www.mydomain.com it gives a 404 Error. I need http://www.mydomain.com/Home view loaded by default, I would like to avoid using a redirect method as it is not SEO friendly...
If you haven't chanegd the RegisterRoutes in the Global.asax.cs file, then it defaults to /Home/Index
see below.
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
So the 4th line of code is where the defaults are configured.
As for the default setting, you can use the RegisterRoutes(RouteCollection routes) method that is available in Global.asax.cs to mark the default controller and its corresponding action method. Regarding hitting www.mydomain.com and getting a 404 error, I need more information. if you are on windows and working on a developer machine, did you add the entry to hosts file located in
C:\Windows\System32\drivers\etc

Subdomain Points To Blog Folder (Non-MVC) on an MVC 3 Site

I have an MVC 3 site that is working and currently quite basic. There is a folder off of the root called Blog where I have BlogEngine.net setup. Both work as I want. I had to do the following code in the global.asax file to make sure that MVC would ignore any request going to the Blog folder as follows:
public static void RegisterRoutes(RouteCollection routes) {
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{folder}/{*pathinfo}", new { folder = "Blog" });
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home",
action = "Index",
id = UrlParameter.Optional } // Parameter defaults
);
}
What I am looking for is to be able to go to: blog.mysite.com and have it go directly to the /blog folder. I have a Subdomain setup through my hosting provider. However, I am not sure what to do beyond this to tell it to go anywhere. When I currently go to blog.mysite.com, it just takes me to the home page.
I suspect I will have to add a MapRoute assume it will be smart enough to still Ignore the {folder} one.
I think the answer to this question might help you along:
Is it possible to make an ASP.NET MVC route based on a subdomain?

ASP.NET MVC App Routing Not Working For Dynamic Data WebForm Pages

I need the correct Global.asax settings in order for my Dynamic Data site to run under an ASP.NET MVC project. The routing currently appears to be my issue.
Here is my global.asax:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
MetaModel model = new MetaModel();
model.RegisterContext(typeof(Models.DBDataContext), new ContextConfiguration() { ScaffoldAllTables = true });
routes.Add(new DynamicDataRoute("DD/{table}/{action}.aspx") {
Constraints = new RouteValueDictionary(new { action = "List|Details|Edit|Insert" }),
Model = model
});
routes.MapRoute(
"Assignment",
"Assignment/{action}/{page}",
new { controller = "Assignment", action = "Index", page = "" });
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Admin", action = "Index", id = "" }); // Parameter defaults
}
Link that I'm trying to use is:
http://localhost:64205/DD/Work_Phases/ListDetails.aspx
I am getting the following message:
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:
/DD/Work_Phases/ListDetails.aspx
I've tried replacing DD with DynamicData since the folder inside of the app is DynamicData and that yielded the exact same result.
The URL
http://localhost:64205/DD/Work_Phases/ListDetails.aspx
is matching your second (default) route, which is trying to hit a controller called "DD".
You may need another route entry that looks something like this:
routes.MapRoute(
"DD",
"DD/{action}/{page}",
new { controller = "NameOfController", action = "Index", page = "" }
);
...although I can't imagine why you would need to pass a page parameter. The page view that is hit depends on the return action of the controller method.
For a better look at integrating Dynamic Data with ASP.NET MVC, have a look at Scott Hanselman's Plugin-Hybrids article. He has some details about handling the .ASPX files that are not part of MVC. In particular, if you have an .ASPX that you don't want to be processed by the ASP.NET MVC controllers, you can install an Ignore Route:
routes.IgnoreRoute("{myWebForms}.aspx/{*pathInfo}");
It should be noted that ASP.NET MVC is configured out of the box to ignore URL requests for files that physically exist on the disk, although Scott's IgnoreRoute technique is apparently more efficient.
The url doesn't match your dynamic data route because it doesn't fit the constraints you put on it. You're requesting action ListDetails but only these actions are allowed
Constraints = new RouteValueDictionary(
new { action = "List|Details|Edit|Insert" }
EDIT: are you sure that an action called ListDetails exists? Then modify the constraints above to
Constraints = new RouteValueDictionary(
new { action = "ListDetails|List|Details|Edit|Insert" }
Just to be sure that it's the constraints that's causing the route to be ignored, can you try one of the default actions? E.g.
http://localhost:64205/DD/Work_Phases/List.aspx
For ASP.NET MVC to work, you will have to match the URL you are trying to access with the list of routes.
For your current global.asax, example of valid URLs are:
http://domain/AnyController/AnyAction/AnyParameter
http://domain/Assignment/
http://domain/Assignment/AnyAction/AnyParameter
MVC requests are redirected to the proper Controller class, Action method, with parameters as passed in. MVC request is not redirected to any ASPX class. This is the difference between ASP.NET MVC and vanilla ASP.NET Page.

Default.aspx not getting executed in ASP.NET project with MVC sections

My app is mainly an ASP.NET app that I'm adding an MVC section to it.
My Default.aspx (no codebehind) page has a simple Response.Redirect to a StartPage.aspx page but for some reason MVC is taking over and I'm not getting to the StartPage.aspx page. Instead I get routed over to my first and only MVC section which is a registered route that I've registered in the global.asax.cs page (Albums).
Is there a way to tell MVC to leave my requests to the root "/" to be my IIS 7 default document...in this case Default.aspx?
This is what is in my RegisterRoutes:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("Albums","{controller}/{action}/{id}",
new { controller = "Albums", action = "Index", id = "" });
If you remove the default controller from your second route there, it won't match against "/" anymore and Routing will ignore requests for "/", leaving them for the usual ASP.Net pipeline to handle
So, change your routes to:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("Albums","{controller}/{action}/{id}",
new { action = "Index", id = "" });
That should solve your problem!
The default.aspx page is being served by IIS because it is the default document. MVC would
let the default.aspx page handle the request, if it realized that the request was for default.aspx (e.g. "http://foo.com/default.aspx"). It doesn't relize that though in this scenario ("http://foo.com") so you could add this before the default route to achieve what you are after
// ignore "/"
routes.IgnoreRoute("");
// default route
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index",
id = UrlParameter.Optional } // Parameter defaults
);
You could tell the MVC to ignore the Default.aspx like this:
routes.IgnoreRoute("Default.aspx");

Resources