In ASP.Net MVC routing, how can you route 2 different paths that look the same, but have different types? - asp.net-mvc

In ASP.Net MVC, I want 2 different routes:
http://mysite.com/foo/12345
and
http://mysite.com/foo/bar
In the class Foo, I have 2 methods that return ActionResult
public ActionResult DetailsById(int id)
{
. . . some code
}
and
public ActionResult DetailsByName(string name)
{
. . . some code
}
How do I set up 2 routes so that if the parameter is an int, it goes to DetailsById, but otherwise goes to DetailsByName?

You can use a route constraint for the first route.
routes.MapRoute("DetailsById",
"foo/{id}",
new { controller = "foo", action = "DetailsById" },
new { id = #"\d+" } // Parameter constraints
);
routes.MapRoute("DetailsByName",
"foo/{id}",
new { controller = "foo", action = "DetailsByName" }
);
The first route will only accept ids that match the regex (which accepts numbers only). If it doesn't match the first route, it will go to the second.

Use something like this:
routes.MapRoute(
"DetailsById",
"Foo/{Id}",
new {controller="Foo", action="DetailsById"},
new {Id= #"\d+" }
);
routes.MapRoute(
"DetailsByName",
"Foo/{Name}",
new {controller="Foo", action="DetailsByName"}
);
Remember that the routes are checked from top to bottom and stop at the first match.

I'm assuming that you already have a default route set up for your id parameter.
The only thing you will need to do is add a map route in your global.asax.cs:
routes.MapRoute(
"Foo_DetailsByName",// Route name
"Foo/DetailsByName/{name}",// URL with parameters
new { controller = "Foo", action = "DetailsByName", name = String.Empty } // Parameter defaults
);

In some cases, this can be accomplished through a route constraint. A common scenario is the ability to have my domain.com/482 behave the same way as my domain.com/products/details/482, where you do not want the 482 to be matched as a controller but as a Product ID.
Route constraints are regular expressions, though, so while you can use regex to match the pattern of the route, you are not actually matching based on data type.
See: http://www.asp.net/mvc/tutorials/creating-a-route-constraint-cs

Related

MVC routing a repeatable pattern?

A design goal for a website I'm working on is to keep the URL in the browser in a state where the user can copy it, and the link can be used from another browser/user/machine to return to the spot that the url was copied. (The actual changes will happen via AJAX, but the URL will change to reflect where they are.)
Example: If you were on the customer page looking at customer 123, and had details pulled up on their order #456, and full details on line 6 of this order, your url could simply be /customer/123/456/6
The challenge comes with a second feature: Users can add UI columns (analogous to adding a new tab in a tab view, or a new document in an MDI app) Each column can easily generate a routable url, but I need the url to reflect one or more columns. (E.G. User has both /customer/123/456/6 and /customer/333/55/2 in two side by side columns)
In a perfect world, I'd like the url to be /customer/123/456/6/customer/333/55/2 for the above scenario, but I don't know if MVC routing can handle repetitive patterns, or, if so, how it is done.
Can this be done via routing? If not is there a way to get this type of one-or-more functionality from Url?
You could create a custom route handler (see my previous answer) or derive from a RouteBase like NightOwl888 suggested. Another approach would be to simply use a model binder and a model binder attribute.
public class CustomerInvoiceLineAttribute : CustomModelBinderAttribute
{
public override IModelBinder GetBinder()
{
return new CustomerInvoiceLineModelBinder();
}
}
public class CustomerInvoiceLineModelBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var path = (string)bindingContext.ValueProvider.GetValue(bindingContext.ModelName).AttemptedValue;
var data = path.Split(new[] { "/customer/" }, StringSplitOptions.RemoveEmptyEntries);
return data.Select(d =>
{
var rawInfo = d.Split('/');
return new CustomerInvoiceLine
{
CustomerId = int.Parse(rawInfo[0]),
InvoiceId = int.Parse(rawInfo[1]),
Line = int.Parse(rawInfo[2])
};
});
}
}
You define your route by specifying a star route data. This mean that the route parameter will contains everything following the action
routes.MapRoute(
name: "CustomerViewer",
url: "customer/{*customerInfo}",
defaults: new { controller = "Customer", action = "Index" });
Then in your controller, you bind your parameter with the same name as the star route parameter using the custom model binder defined above:
public ActionResult Index([CustomerInvoiceLine] IEnumerable<CustomerInvoiceLine> customerInfo)
{
return View();
}
You will need to add validation during the parsing and probably security too, so that a customer cannot read the invoice of other customers.
Also know that URL have a maximum length of 2000 characters.
You can do this with the built-in routing as long as you don't anticipate that any of your patterns will repeat or have optional parameters that don't appear in the same segment of the URL as other optional parameters.
It is possible to use routing with optional parameters by factoring out all of the permutations, but if you ask me it is much simpler to use the query string for this purpose.
NOTE: By definition, a URL must be unique. So you must manually ensure your URLs don't have any collisions. The simplest way to do this is by matching the page with the path (route) and adding this extra information as query string values. That way you don't have to concern yourself with accidentally making routes that are exactly the same.
However, if you insist on using a route for this purpose, you should probably put your URLs in a database in a field with a unique constraint to ensure they are unique.
For the most advanced customization of routing, subclass RouteBase or Route. This allows you to map any URL to a set of route values and map the route values back to the same URL, which lets you use it in an ActionLink or RouteLink to build the URLs for your views and controllers.
public class CustomPageRoute : RouteBase
{
// This matches the incoming URL and translates it into RouteData
// (typically a set of key value pairs in the RouteData.Values dictionary)
public override RouteData GetRouteData(HttpContextBase httpContext)
{
RouteData result = null;
// Trim the leading slash
var path = httpContext.Request.Path.Substring(1);
if (/* the path matches your route logic */)
{
result = new RouteData(this, new MvcRouteHandler());
result.Values["controller"] = "MyController";
result.Values["action"] = "MyAction";
// Any other route values to match your action...
}
// IMPORTANT: Always return null if there is no match.
// This tells .NET routing to check the next route that is registered.
return result;
}
// This builds the URL for ActionLink and RouteLink
public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
{
VirtualPathData result = null;
if (/* all of the expected route values match the request (the values parameter) */)
{
result = new VirtualPathData(this, page.VirtualPath);
}
// IMPORTANT: Always return null if there is no match.
// This tells .NET routing to check the next route that is registered.
return result;
}
}
Usage
routes.Add(
name: "CustomPage",
item: new CustomPageRoute());
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

Redirect To Action method not mapping correctly

I'm calling RedirectToAction but it isn't working properly.
I want the resulting URL to look like this:
https://localhost:44301/ManageSpaces/123/overview
but it looks like this and is missing the action portion of the URL:
https://localhost:44301/ManageSpaces/123
Here is my RedirectToAction call.
return RedirectToAction("overview", new RouteValueDictionary(
new {controller = "ManageSpaces", action = "overview", id = 123}));
Here is what my route looks like in RouteConfig:
routes.MapRoute("ManageSpaces",
"ManageSpaces/{id}/{action}",
new { controller = "ManageSpaces", action = "overview"},
new { id = #"\d+" } //The regular expression \d+ matches one or more integers
);
Maybe it is taking the default route. Rename, remove, or comment out the default route to see if that has any effect.
You have made your action route value optional by providing a default value. Optional values are ignored when resolving the URL.
routes.MapRoute("ManageSpaces",
"ManageSpaces/{id}/{action}",
new { controller = "ManageSpaces", action = "overview"},
new { id = #"\d+" } //The regular expression \d+ matches one or more integers
);
If you want to include the action in the URL, you have to make it a required argument.
routes.MapRoute("ManageSpaces",
"ManageSpaces/{id}/{action}",
new { controller = "ManageSpaces"},
new { id = #"\d+" } //The regular expression \d+ matches one or more integers
);

If action name found then call action, if not call a default action in MVC Routing

this is my current routing
routes.MapRoute(
"Default", // Route name
"{action}/{id}", // URL with parameters
new { controller = "MarketingSite", action = "index", id = "" } // Parameter defaults
);
so whenever I visit domain.com/test
This would invoke the test action in the controller called MarketingSite.
What I want to achieve is when I visit domain.com/load-from-db and since I don't have an action called load-from-db, I want to direct the request to a specific action and load-from-db becomes the parameter for that action. In that action, i'm going to read something from the database based on the parameter and then return a view. If I specify an action that exist, then it would just call that action.
Any help?
First note - the routing engine will apply the routes in the order you list them, checking each, and continuing if it cannot match. You currently have the "default" route setup as a catch-all - if is doesn't find a controller it uses "MarketingSite", if there is no action it uses "index". If I am understanding your problem, you don't want an "Index" action at all, you want to call another action, and pass the query to that.
You could try:
//Look for a matching action
routes.MapRoute(
"MatchAction", // Route name
"{action}/{id}",
new { controller = "MarketingSite", id = UrlParameter.Optional}
);
//With a single segment, pass that to a specific action as a parameter.
routes.MapRoute(
"load-from-db", // Route name
"{load-from-db}", // URL with parameters
new { controller = "MarketingSite", action = "MyAction"}
);
//With no segments (ex. domain.com/) use the Index on MarketingSite as default.
routes.MapRoute(
"Default", // Route name
"", // URL with parameters
new { controller = "MarketingSite", action = "Index"}
);
However, these routes might not accomplish exactly what you are looking for - getting your routes mapped in MVC can be a bit tricky. You should check this this out, and design your routes accordingly.
Hope that helps,
Chris
Override the HandleUnknownAction method on the MarketingSite controller:
protected override void HandleUnknownAction(string actionName) {
// TODO: check for actionName in database
}

Generic ASP.NET MVC Route Conflict

I'm working on a Legacy ASP.NET system. I say legacy because there are NO tests around 90% of the system. I'm trying to fix the routes in this project and I'm running into a issue I wish to solve with generic routes.
I have the following routes:
routes.MapRoute(
"DefaultWithPdn",
"{controller}/{action}/{pdn}",
new { controller = "", action = "Index", pdn = "" },
null
);
routes.MapRoute(
"DefaultWithClientId",
"{controller}/{action}/{clientId}",
new { controller = "", action = "index", clientid = "" },
null
);
The problem is that the first route is catching all of the traffic for what I need to be routed to the second route. The route is generic (no controller is defined in the constraint in either route definition) because multiple controllers throughout the entire app share this same premise (sometimes we need a "pdn" sometimes we need a "clientId").
How can I map these generic routes so that they go to the proper controller and action, yet not have one be too greedy? Or can I at all? Are these routes too generic (which is what I'm starting to believe is the case).
My only option at this point (AFAIK) is one of the following:
In the contraints, apply a regex to match the action values like: (foo|bar|biz|bang) and the same for the controller: (home|customer|products) for each controller. However, this has a problem in the fact that I may need to do this:
~/Foo/Home/123 // Should map to "DefaultwithPdn"
~/Foo/Home/abc // Should map to "DefaultWithClientId"
Which means that if the Foo Controller has an action that takes a pdn and another action that takes a clientId (which happens all the time in this app), the wrong route is chosen.
To hardcode these contstraints into each possible controller/action combo seems like a lot of duplication to me and I have the feeling I've been looking at the problem for too long so I need another pair of eyes to help out.
Can I have generic routes to handle this scenario? Or do I need to have custom routes for each controller with constraints applied to the actions on those routes?
Thanks
Add constraints to your routes by removing the null and replacing it with the constraint needed for that route:
For PDN, use a regular expression for digits:
routes.MapRoute(
"DefaultWithPdn",
"{controller}/{action}/{pdn}",
new { controller = "", action = "Index", pdn = "" },
new { pdn = #"\d+" }
);
For ClientID, use a regular expression for all characters:
routes.MapRoute(
"DefaultWithClientId",
"{controller}/{action}/{clientid}",
new { controller = "", action = "index", clientid = "" },
new { clientid = #"[A-Za-z]+" }
);
Since I don't keep the minutia of regular expression stuff in my head, I generally use a cheat sheet.
You should add some route constraints that say the PDN route matches numbers and the ClientId matches strings
I usually create a series of matches to use throughout my route declaration like so:
readonly static string ALPHA_MATCH = #"[\da-zA-Z]";
readonly static string DIGIT_MATCH = #"\d+";
then add the constraints to the routes like this:
routes.MapRoute(
"DefaultWithPdn",
"{controller}/{action}/{pdn}",
new { controller = "", action = "Index", pdn = "" },
new { pdn = DIGIT_MATCH }
);
routes.MapRoute(
"DefaultWithClientId",
"{controller}/{action}/{clientId}",
new { controller = "", action = "index", clientid = "" },
new { clientId = ALPHA_MATCH }
);

Can I constrain a route parameter to a certain type in ASP.net MVC?

I have the following route:
routes.MapRoute(
"Search", // Route name
"Search/{affiliateId}", // URL with parameters
new { controller = "Syndication", action = "Search" } // Parameter defaults
);
Is there a way I can ensure "affiliateId" is a valid Guid? I'm using MVCContrib elsewhere in my site and I'm fairly it provides a way to implement this kind of constraint.... I just don't know what it is!
You could write regex constraints:
routes.MapRoute(
"Search", // Route name
"Search/{affiliateId}", // URL with parameters
new { controller = "Syndication", action = "Search" }, // Parameter defaults
new { affiliateId = "SOME REGEX TO TEST GUID FORMAT" } // constraints
);
I've never heard of this. I fear it would cause some confusion if you mistakenly used the wrong type for the affiliateId parameter in one of your action methods.

Resources