ASP.NET MVC enum argument in controller mapping - c#

ASP.NET MVC provides simple templates for controller methods such as Details, and can have something like:
public ActionResult Details(int id)
{
// do something
}
This can be accessed by: http://localhost:port/Controller/Details/id
What I'm trying to do is instead provide a different type like:
public enum MyEnum
{
All,
Pending,
Complete
}
And then I setup my controller method like:
public ActionResult MyMethod(MyEnum myEnum = MyEnum.Pending)
{
// do something
}
This works fine for: http://localhost:port/Controller/MyMethod/ because it uses the default argument.
To specify a different argument I have to do http://localhost:port/Controller/MyMethod?myEnum=All and that works.
I'm wondering, is it possible for me to be able to do http://localhost:port/Controller/MyMethod/All instead of using ?myEnum=All?
Upon trying to do it that way I get a 404 exception which is understandable, but why doesn't this happen for id in Details?
Can I change the MapRoute which is currently: url: "{controller}/{action}/{id}" to allow me to achieve it with my own type?
What I've tried so far:
I only want this route enforcement for one of my schemes such as http://localhost:port/Controller/MyMethod/{ViewType}, I tried this but it doesn't seem to do anything:
routes.MapRoute(
"MyRoute",
"MyController/Index/{MyEnum}",
new { controller = "MyController", action = "Pending" }
);

/Controller/MyMethod/All will actually work. The problem is with the default route, which will consider All to be the id route parameter, which doesn't line up with what your action is using as a parameter. It would actually work fine if your action signature was:
public ActionResult MyMethod(MyEnum id = MyEnum.Pending)
Since it will then bind All to the right thing.
You could add another route for this use-case, but you'll need to be careful that you don't just create another "default" route, which will take over. In other words, you'll have to fix part of the URL:
routes.MapRoute(
"MyCustomRoute",
"Controller/{action}/{myEnum}",
new { controller = "Controller", action = "MyMethod", myEnum = MyEnum.Pending }
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Then, by the mere presence of the /Controller/ prefix to the route, it will use your custom route instead, and fill in All for the myEnum param, rather than hitting the default route and try to fill in id.
However, be advised that when using enums as route params, they must be exact matches. So, while /Controller/MyMethod/All will work, /Controller/MyMethod/all will not. To get around this, you'll have to create a custom model binder. I did a quick search and found the following article which may help you in that regard.

You can indeed. Do not change the default route "{controller}/{action}/{id}", but rather add one before the default. This new one needs to be fairly specific:
routes.MapRoute(
"EnumRoute",
"Controller/MyMethod/{myEnum}",
new { controller = "Controller", action = "MyMethod", myEnum = UrlParameter.Optional }
);
What that basically says is "when you see request to literally Controller/MyMethod/whatever, use this controller and that method and pass whatever as parameter of the request". Note that actual controller does not necessary have to be what route says in the url, although it is a good idea to stick to that.

Related

ASP.NET MVC new Routes not working

I am using ASP.NET MVC and I am trying to create a new route for a parameter like so:
config.Routes.MapHttpRoute(
name: "MarkOnline",
routeTemplate: "api/{controller}/{id}",
defaults: new { offline = RouteParameter.Optional }
);
and here is my method call I am trying to use inside my API Controller
public void MarkOnline(string offline)
{
}
however what gets returned is my Entity Framework GetData method in the API Controller, which is this:
public IQueryable<VistaLCPreview> GetData()
{
return db.Data;
}
What am I doing wrong?
In this context, GetData is being called due to the fact that it has a prefix of Get. There's a convention that maps HTTP GET to functions prefixed with Get, HTTP POST to PostXXX, etc. GetData is being resolved by the default HTTP route, which specifies an optional id parameter and is not present in your expected GetData URL example (which is what you want there).
The MapHttpRoute from your example is not going to match, due to the id parameter in the routeTemplate, which has not been defaulted to RouteParameter.Optional. This route is actually unnecessary - You do not need to include query-string parameters in this route definition. Query-string parameters are simply mapped into the arguments passed into the actions (offline in your case).
Because MarkOnline is not prefixed with one of the HTTP Verbs as I mentioned above, it is not being matched by the default HTTP route. To fix your problem you simply need to do two things:
Remove the MapHttpRoute that you added. This is not needed as the default HTTP route I've already mentioned will cover your use-case.
Add the HttpGet attribute to your MarkOnline method. This will cause the routing to pick up MarkOnline when an offline query-string parameter is found, but call GetData when it is not.
Your route is not configured correctly, you are not specifying the default action on your controller.
It should be something like this:
routes.MapRoute(
name: "MarkOnlineRoute",
url: "api/{controller}",
defaults: new { action = "MarkOnline" }
);
But also notice that the order which you configure your routes is important, it should be located before the default route configuration:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
And as a reference, this is my test controller:
public class AvailabilityController : Controller
{
// GET: MarkOnline
public void MarkOnline(string offline)
{
//return Json(new { isOnline = true, name=offline }, JsonRequestBehavior.AllowGet);
}
}
And it is called with: http://localhost/api/availability?offline=xxx#xxx.com

Multiple controllers, same same controller name

I was wondering if this is possible. Say I have a monolithic Controller, ReportController.cs.
I want to make a totally separate controller file but still keep the /Report/ in the url that we've some to know and expect.
What I tried was this in my global asax:
routes.MapRoute(
"Testing", // Route name
"{test}/{action}/{id}" // URL with parameters
);
and I added a new Controller named ReportTest.cs
the original route looks like this:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "LandingPage", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Every time I try to call the simple action I have in ReportTest.cs I get this error: "Value cannot be null or empty. Parameter name: controllerName"
Am I misunderstanding how this works. When you have "{controller}/.." is this not saying 'look for any controllers named + controller and use that'. So if I go to .../Report/DoStuff it'll look for the method DoStuff on ReportController right?
So wouldn't my other route just append a search sequence? So if I put .../Report/DoStuff it'll look for the method DoStuff on ReportController and ReportTest right?
The routing format string:
{controller}/{action}/{id}
Means: the first part ("part" being "element after splitting on /") of the request URI is the controller name, the next part the action method and the last part the ID.
The placeholders {controller} and {action} are special. So your route {test}/{action}/{id} will not find any controller, as none is specified, and {test} doesn't mean anything. (Well it does, it'll get added as a route attribute named "test", and assigned a value representing that part of the request URI, so that is irrelevant for this scenario).
If you want to route an URI to a controller that is not mentioned in the URI, then you must literally specify the prefix, and the controller it should be routed to.
So your routing will look like this:
routes.MapRoute(
"SecondReportController",
"Report/NotOnReportController/{id}",
new { controller = "NotAReportController" }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "LandingPage", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Then you can use the following controller:
public class NotAReportController : Controller
{
public ActionResult NotOnReportController(int id)
{
// ...
}
}
You can of course also use attribute routing instead:
public class NotAReportController : Controller
{
[Route("Report/NotOnReportController/{id}")]
public ActionResult NotOnReportController(int id)
{
// ...
}
}

How to distinguish between two routes: one for parameterless action and one for the index action with one parameter?

I have a controller with multiple actions and I set up the following routes:
routes.MapRoute(
name: "MyCustomRoute",
url: "MyTarget/{option}",
defaults: new { controller = "MyTarget", action = "Index", option = "" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
The main idea here is to call the Index action of the MyTarget controller as default, passing only the argument in the URL.
The lightweight controller looks like this:
public class MyTargetController : Controller
{
public ActionResult Index(string option)
{ ... }
public ActionResult FirstAction()
{ ... }
public ActionResult SecondAction(param list)
{ ... }
}
The MyCustomRoute is set to map MyWebsite/MyTarget/randomOption to the Index action, passing randomOption as the option parameter. The problem is that this route catches all the other actions too: MyWebsite/MyTarget/FirstAction and MyWebsite/MyTarget/SecondAction (ignore the lack of parameters) are mapped to the Index action and their names are routed as the option parameter.
I don't want to change the URL into something like MyWebsite/MyTarget/Index/randomOption. Is there a clear way for distinguishing between a default action with one parameter and other actions, which may or may not have parameter?
EDIT: the following workarounds can be implemented, balancing the advantages and disadvantages:
all actions except Index can be moved to a helper controller: creates two separate controllers processing the same logic;
a custom route constraint can be created, that checks if the parameter value corresponds to the name of an existing action (except Index): needs a way of storing the names of the actions and needs the list to be updated every time a new action is added to the controller (Reflection might be a better approach).
None of the above workarounds seam to be elegant and without any "special" caring.

How to set up routing so that Index does show?

So I know google can penalize a site if you have the same content on multiple urls... unfortunately, in MVC this is too common i can have example.com/, example.com/Home/ and example.com/Home/Index and all three urls would take me to the same page... so how do I make sure that whenever Index is in the url, that it redirects to the same without the Index and of course the same thing with the Home
Perhaps this little library may be useful for you.
This library is not very convinient in your case, but it should work.
var route = routes.MapRoute(name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });
routes.Redirect(r => r.MapRoute("home_index", "/home/index")).To(route);
routes.Redirect(r => r.MapRoute("home", "/home")).To(route);
The way I handle this is for default pages like Index is to simply create an explicit route for only one of them. I.e. "example.com/People" would be the route for People/Index, and there would be no valid page at the url "/example.com/People/Index".
The Home example is unique in that it has potentially three different URLs. Again in this case I'd simply create a route for "example.com" for that Index action, and not support the other two urls. In other words, you would never link to the other forms of the URL, so their absence should never cause a problem.
We use a Nuget package called AttributeRouting to support this. When you specifiy a GET route for a page, it overrides the defaults for MVC.
Using AttributeRouting usually you'd map the index to [GET("")] but for the special case of Home where you also want to also support the root URL that omits the controller name , I think you'd also add an additional attribute with IsAbsoluteUrl:
public class HomeController : BaseController
{
[GET("")]
[GET("", IsAbsoluteUrl = true)]
public ActionResult Index()
{...
So I found a way to do it without any external Library...
In my RouteConfig I had to add these two routes at the top, just below the IgnoreRoute
routes.MapRoute(
"Root",
"Home/",
new { controller = "Redirect", action = "Home" }
);
routes.MapRoute(
"Index",
"{action}/Index",
new { controller = "Redirect", action = "Home" }
);
Then I had to create a new Controller called Redirect and I created a method for each of my other Controllers like this:
public class RedirectController : Controller
{
public ActionResult Home()
{
return RedirectPermanent("~/");
}
public ActionResult News()
{
return RedirectPermanent("~/News/");
}
public ActionResult ContactUs()
{
return RedirectPermanent("~/ContactUs/");
}
// A method for each of my Controllers
}
That's it, now my site looks legit. No more Home, no more Index in my URLs, this of course has the limitation of not being able to accept parameters to any of the Index methods of your Controllers though if it was really necessary, you should be able to tweak this to achieve what you want.
Just an FYI, if you want to pass an argument to your Index Action, then you can add a third route like this:
routes.MapRoute(
name: "ContactUs",
url: "ContactUs/{id}/{action}",
defaults: new { controller = "ContactUs", action = "Index", id = UrlParameter.Optional }
);
This will create a URL like this: /ContactUs/14

How to define route & controller structure for 2 controllers?

I want to create an MVC app to generate factsheets, but I'm not sure how to structure the routing and controllers.
It consists of an index page, which acts as a template for the layout of a number of independent panels, each of which contains different types of data.
I want to have a the route template like the following:
/Factsheets/Panels/PanelType?fundId=1&countryId=ABC
so these would be the URLs I'm using:
/Factsheets/Panels/NameAndDatePanel?Afund=1&county=IE
/Factsheets/Panels/AssetsPanel?fund=1&county=IE
I want a Factsheets controller to be able to supply the Panel controller with the configuration object it needs to generate the type of panel I request.
What should my routing structure look like?
What should my controller structure look like?
edit:
What changes if I want to have a roure structure as follows:
I want to have a the route template like the following:
/Factsheets/ContentArea/Panels/PanelType?fundId=1&countryId=ABC
so these would be the URLs I'm using:
/Factsheets/PageTop/Panels/NameAndDatePanel?Afund=1&county=IE
/Factsheets/PageTop/Panels/AssetsPanel?fund=1&county=IE
so that FactsheetsController is instantiated to contain the data needed by a ContentArea to know which data to supply to the panel it needs to generate.
What comes after the ? is the query string so it won't be reflected by the routing engine. So your route might look like this:
routes.MapRoute(
"Default",
"{controller}/{action}/{panelType}",
new { controller = "Factsheets", action = "Panels", panelType = UrlParameter.Optional }
);
And you would have the following controller:
public class FactsheetsController : Controller
{
public ActionResult Panels(string panelType)
{
return View();
}
}
UPDATE:
If the panelType parameter is not optional you could provide a regex constraint:
routes.MapRoute(
"Default",
"Factsheets/Panels/{panelType}",
new { controller = "Factsheets", action = "Panels", panelType = UrlParameter.Optional },
new { panelType = ".+" }
);

Categories

Resources