Reserved ASP.NET MVC Controller Names? - asp.net-mvc

So I just spent what felt like an hour debugging why I was receiving a client-side 404 and a server-side System.Web.Http.HttpResponseException on System.Web.Http.dll!System.Web.Http.Dispatcher.DefaultHttpControllerSelector.SelectController(System.Net.Http.HttpRequestMessage request) + 0x33f byte; no matter what I tried I could not get my ApiController derivative to receive requests.
In the end, after trying to convert it to a regular Controller to no effect, it occurred to me to change the name from APIController to APIXController only to have everything work.
Where is it stated that API is a reserved controller name?
Where is the full list of reserved controller names?
Is it possible to bypass this restriction on using APIController as my class name, and if so, how do I go about doing so?
One of the first things I checked was my route configuration, which is the bog standard
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

APIController is not a reserved name. But, if you're using a standard MVC4 template, then by default it includes an instance of a WebAPI stack, and this registers a route that starts with /api
You were most likely running into that.
Look under AppStart and in the WebApiConfig.cs file.
By the way, the key debugging symptom is anything in System.Web.Http, that is used by WebApi, and would not show up in an MVC app.

Related

Mapping controllers/action in route table

Is it necessary to map all the controllers/actions to the route table in MapRoute function in an application ?
If not so, then how they added to the route tables ?
Using the default convention-based route will cover basic routing for many cases, especially those that aren't concerned so much with what the URL looks like. Since there is only 1 route in the route table, this is the best performing option, and it automatically "just works" for any controller or action that is added to the project.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Typically, other than that URLs are statically added either by using the MapRoute method or [Route] attributes, meaning they are compiled into the application and cannot change at runtime. This makes routes testable so the configuration can be verified before release.
However, if the goal is to make some sort of CMS where the application has control over the URLs and you want them to automatically be "made live" as you add records to your database you can extend RouteBase to handle that (along with virtually any other) routing scenario.

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.

404 asp.net mvc - beginner question in routing

This is a beginner level question for asp.net MVC
I have the following code in global.asax.cs
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 = (string)null } // Parameter defaults
);
}
in Homecontroller.cs i have updated the Index method as follows
public ActionResult Index(string id)
{
ViewData["Message"] = "Welcome to ASP.NET MVC1!"+ id;
return View();
}
My understanding is, if I give the url http://localhost/mvc1/default/1 it should work
instead it is throwing up 404 error
any help what is the reason behind this
I'm assuming your application is called "mvc1" and that's the root of your project. If that's the case:
So "default" is the name if your route, not the name of the action. Basically what the routing engine does is look for a controller and action that matches requests coming in. Given the route you have setup, it would break down like this:
http://localhost/MVCApplication1/default/1
(cont) (action)
If certain parts of the route are omitted, it will attempt to fill in the missing values with the defaults you have specified. As you can see, there is no controller named DefaultController in your project, and thus it uses the default you've specified which is Home. It then tries to find an action method called default and fails again, so it uses the default value in your route, which is Index. Finally, you have 2 segments left in your URL, and no route matches that pattern (2 segments after the action), so it can't find the right place to go.
What you need to do is remove one of your segments, and this should work. Routing can be a little tricky, so I would recommend reading up on it.
The URL you're requesting is asking for a controller called "mvc1" and an action called "default" which will receive an id of "1". Since you don't have a controller named "mvc1" (I assume?), you're getting the 404 error.
The defaults for controller and action are only used if controller and action aren't provided. Since you provided controller and action, MVC is looking for them specifically.

Legacy URL rewriting with query string parameters

I've looked at ASP.Net MVC routing legacy URLs passing querystring Ids to controller actions and several other similar posts for legacy URL routing, but I can't get past the error "The RouteData must contain an item named 'controller' with a non-empty string value." Looking this up on line didn't give me any hints to solve my problem.
I've implemented the Legacy routing class described in the link above, and this is what I've defined in the routing table:
routes.Add(
"Legacy",
new LegacyRoute("fooref.aspx",
"FooRef",
new LegacyRouteHandler())
);
routes.MapRoute(
"FooRef",
"{controller}/{action}",
new
{
controller = "Home",
action = "Index",
foo_id = UrlParameter.Optional,
bar_id = UrlParameter.Optional
}
);
When I use Phil Haack's route debugger, it indicates that fooref.aspx has a match, but when I turn the route debugger off, I get the error above. If I reverse the statement order, I get "Resource not found" for /ctprefer.aspx, which makes sense -- so it appears to be finding that as a valid route when put in the other order.
Where do I need to declare this missing controller reference?
Have routing requirements changed for ASP.NET MVC 2 RTM?
The solution to this is to use an IHttpHandler directly, rather than an MVCHandler. I have posted code that works with MVC 3 to my blog: http://www.olsonsoft.com/blogs/stefanolson/post/Handling-Legacy-Urls-in-AspNet-MVC-3.aspx
...Stefan
You might want to take a look at URL Rewrite module for IIS. It can be used to translate legacy URL to your new MVC URLs without 'polluting' your app with legacy routes.
Don't know if it will fit your solution but it's worth having an alternative.

ASP.NET MVC Routing Questions

I just started using ASP.NET MVC and I have two routing questions.
How do I set up the following routes
in ASP.NET MVC?
domain.com/about-us/
domain.com/contact-us/
domain.com/staff-bios/
I don't want to have a controller specified in the actual url in order to keep the urls shorter. If the urls looked liked this:
domain.com/company/about-us/
domain.com/company/contact-us/
domain.com/company/staff-bios/
it would make more sense to me as I can add a CompanyController and have ActionResults setup for about-us, contact-us, staff-bios and return appropriate views. What am I missing?
What purpose does the name "Default" name have in the default routing rule in Global.asax? Is it used for anything?
Thank you!
I'll answer your second question first - the "Default" is just a name for the route. This can be used if you ever need to refer to a route by name, such as when you want to do URL generation from a route.
Now, for the URLs that you want to set up, you can bypass the controller parameter as long as you're ok with always specifying the same controller as a default. The route might simply look like this:
{action}/{page}
Make sure that it's declared after your other routes, because this will match a lot of URLs that you don't intend to, so you want the other routes to have a crack at it first. Set it up like so:
routes.MapRoute(null, "{action}/{page}",
new { controller = "CompanyController", action = "Company", page = "contact-us" } );
Of course your action method "Company" in your MyDefault controller would need to have a "string page" parameter, but this should do the trick for you. Your Company method would simply check to see if the View existed for whatever the page parameter was, return a 404 if it didn't, or return the View if it did.
Speaking of setting up routes and Phil Haack, I have found his Route Debugger to be invaluable. It's a great tool for when you don't understand why particular routes are being used in place of others or learning how to set up special routing scenarios (such as the one you've mentioned). It's helped clear up many of the intricacies of route creation for me more than any other resource.
To answer your second question about Global.asax, it an optional file used for responding to application level and session-level events raised by ASP.NET or HTTP modules. The Global.asax file resides in the root directory of the ASP.NET application.If you do not define it assumes you have not defined any application handler or session handler. MVC framework uses the routing engine to which the routing rules are defined in the engine, so as to map incoming URL to the correct controller.
From the controller, you can access the ActionName. If there is no specific controller, it will direct to the default page. The default controller is "Home" with its default action "Index". Refer to MSDN:
http://msdn.microsoft.com/en-us/library/2027ewzw%28v=vs.100%29.aspx
Refer to stackoverflow question
What is global.asax used for?
This is a sample of how a default route should look like
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id =
UrlParameter.Optional }
);

Resources