MVC3 Route Table mistake - c#

I have this RegisterRoute function
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.Add("JsActionRoute", JsAction.JsActionRouteHandlerInstance.JsActionRoute);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
Where the JsActionRoute is a route like this.
public static readonly Route JsActionRoute = new Route("JsAction",
new JsAction.JsActionHandler());
I want that all links to JsAction/ should be handled by my coustom route.
Now when simply calling #Html.ActionLink, Mvc3 creates a link that is related to JsAction, and I can't understand why.
#Html.ActionLink("About","About") -> JsAction?Action=About&Controller=Index

Routes are evaluated in the same order as they are registered. You could use the RouteLink to explicitly specify a route name:
#Html.RouteLink("About", "Default", new { action = "About" })
If you inverse the order of definitions then links will generate correctly but your custom route will not be hit when requesting the JsAction/ url since there is nothing to disambiguate this url from the default route.
You will have to rethink your routes structure so that there are no such conflicts. You could use constraints. Remember that the default route is very eager and if you don't constrain it, it will often take precedence.
One possibility is to define a controller and action for your custom route:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.Add("JsActionRoute", JsAction.JsActionRouteHandlerInstance.JsActionRoute);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
);
}
and then constrain the custom route by specifying a controller and action to be executed:
public static readonly Route JsActionRoute = new Route(
"JsAction",
new RouteValueDictionary(new { action = "JsAction", controller = "Foo" })
new JsAction.JsActionHandler()
);
Another possibility is to inverse the order of definition of your routes and constrain the default route:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new { controller = "^((?!JsAction).)*$", action = "^((?!JsAction).)*$" }
);
routes.Add("JsActionRoute", JsAction.JsActionRouteHandlerInstance.JsActionRoute);
}

Related

MVC: Using the routing attribute with a url that has an additional segment

I have a controller called Search. A normal url would be the following:
localhost:44351/<ClientName>/Search/ByCity
This would hit my ByCity action within my SearchController.
Now however, a url such as the following example, would also need to hit an action within the SearchController:
localhost:44351/<ClientName>/Search/Pharmacy/ByCity
I need to somehow tell my SearchController, if the url contains "Pharmacy/ByCity", to go to the ByCity action.
I've tried using the routing attribute, but my app still hits my old Pharmacy action instead.
In my RouteConfig, I have this:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
}
Then, in my SearchController, I have this:
public virtual ActionResult Pharmacy()
{
//this is an existing action, which gets hit, even when I type in "Pharmacy/ByCity", which is not what I want to happen.
}
[Route("Pharmacy/ByCity")]
public virtual ActionResult ByCity()
{
//this never gets hit
}
Any idea how to have a url containing "Pharmacy/ByCity" to hit my "ByCity" action, rather than "Pharmacy"?
Thanks
It is possible to achieve with the conventional route by set up like below:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Pharmacy",
url: "{clientname}/{controller}/Pharmacy/{action}",
defaults: new { controller = "search" }
);
routes.MapRoute(
name: "Search",
url: "{clientname}/{controller}/{action}",
defaults: new { controller = "search", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Routes are accessed depending on their Order in the routing table.
For conventional routing (RouteConfig.cs), you could add your specific route before the default route.
Remove your Route[] attributes in the controller
Use the code below for RouteConfig
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// add your specific route, before the default route
routes.MapRoute(
name: "SearchByCity", // random name
url: "Search/Pharmacy/ByCity",
defaults: new { controller = "Search", action = "ByCity" }
);
// this is the default route
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
If you want to use Attribute Route, follow steps below.
Remove the default route in RouteConfig.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
}
Then use the controller below, we used RoutePrefix for the controller, and Route for the actions.
[RoutePrefix("Search")]
public class SearchController : Controller
{
[Route("Pharmacy")]
public virtual ActionResult Pharmacy()
{
return View("index");
}
[Route("Pharmacy/ByCity")]
public virtual ActionResult ByCity()
{
return View("index");
}
}

How Can I change the route config to the custom route?

I tried to change and optimize my website URL to the SEO friendly Url. I mean I change the url Like ~/Home/Contact to ~/contact and etc. I change the ~/Home/Index URL to ~/home as well.
When I run my website because I add the attr [Route("~/home")] to my index action application can't find my default route.
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
I don't know how can I change my MapRoute to my new SEO Friendly URL.
I don't want to loss my mvc URL Pattern as well
You can use this class in App_Start folder:
public static class RoutingConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
#region IgnoreRoutes
routes.IgnoreRoute("Content/{*pathInfo}");
routes.IgnoreRoute("Scripts/{*pathInfo}");
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("favicon.ico");
routes.IgnoreRoute("{resource}.ico");
routes.IgnoreRoute("{resource}.png");
routes.IgnoreRoute("{resource}.jpg");
routes.IgnoreRoute("{resource}.gif");
routes.IgnoreRoute("{resource}.txt");
#endregion
routes.LowercaseUrls = true;
routes.MapMvcAttributeRoutes();
// AreaRegistration.RegisterAllAreas();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}",
defaults:
new
{
controller = MVC.Home.Name,
action = MVC.Home.ActionNames.Index,
id = UrlParameter.Optional
},
namespaces: new[] {$"{typeof (RoutingConfig).Namespace}.Controllers"}
);
}
and use this in Global.asax.cs Application_Start method.
RoutingConfig.RegisterRoutes(RouteTable.Routes);
I used from T4MVC nuget package.

ASP.NET Passing Parameter in URL

I wrote a very simple web app in Flask and am porting it to ASP.NET Framework. All the functionality is in JavaScript and HTMl, so the framework should just act as scaffolding. I've got almost everything ported over, except for what seems to be a routing issue. My site expects a string token variable to be appended to the URL, like so: www.mysite.com/token-string. For development, the URL is localhost:*****/string-token, with my Index.cshtml page being displayed as default.
When I pass the URL without the token it works fine and my index page loads. However I get a 404 when I try it with the token. I'm assuming it's identifying the token as a route and is trying to navigate to it? I'm not sure how to fix it. Here are the important parts of my code:
HomeController.cs:
public class HomeController : Controller
{
public ActionResult Index(string token)
{
return View();
}
}
RouteConfig.cs:
NB: I've not changed this, not sure what to do with it.
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
It's quite important that the token is passed in the way it is, rather than as a ? query parameter or anything like that. Additionally, the C# index view doesn't really need to do anything with the token - it gets extracted by the JavaScript.
Any advice is most welcome. Thanks.
Each segment (i.e. {controller}) in the route is a variable, and in the default route makes them all optional. Therefore, your default route is matching the request www.mysite.com/token-string.
What you need to do is insert a route that has a constraint to only match URLs with your token. Assuming your token is a GUID, you could use a regex route constraint as follows:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "TokenRoute",
url: "{token}",
defaults: new { controller = "Home", action = "Index" },
constraints: new { token = #"^[0-9A-Fa-f]{8}[-][0-9A-Fa-f]{4}[-][0-9A-Fa-f]{4}[-][0-9A-Fa-f]{4}[-][0-9A-Fa-f]{12}$" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
If your token is not a GUID, you could either use a different regex or implement IRouteConstraint to ensure the route only matches your tokens. The logic you use could be as simple as a == statement (as shown) or more complex (such as a database lookup).
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "TokenRoute",
url: "{token}",
defaults: new { controller = "Home", action = "Index" },
constraints: new { token = new TokenConstraint() }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
public class TokenConstraint : IRouteConstraint
{
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
if ((string)values[parameterName] == "MyToken")
{
return true;
}
return false;
}
}
Note that you should use the route value key {token} in the url: parameter to match the action method parameter name token.
public ActionResult Index(string token)
{
return View();
}
I guess you could try changing the default route to include token instead of id as shown below.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{token}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
The default Route pattern which you have expects the parameter with name as 'id'
Either add (or modify the default route) like below route pattern
routes.MapRoute(
name: "AnotherRoute", //your desired route name
url: "{controller}/{action}/{token-string}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

Map Custom Route ASP.NET MVC5

I haven't used .NET Routing before.
I have a URL: http://myurl.com/Account/Login/?IsIPA=true.
I want to be able to hit this URL with the following: http://myurl.com/IPA
This is the only custom route I want hit.
Can I create a route just for a single URL like this?
My code that isn't working is:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute("IPA", "Account/Login/{IsIPA}", new { controller = "Account", action = "Login", IsIPA = "true" });
}
I get the error:
The constraint entry IsIPA on the route with route template Account/Login/{IsIPA}=True must have a string value or be of a type which implements System.Web.Routing.IRouteConstraint.
Route matching is similar to a switch case statement. The url parameter and any default values and constraints are all considered to determine whether or not it is a match with the incoming URL. If the route matches, it will then create a dictionary of route values based on the configuration. If the route does not match, the next route in the collection is tried until a match is found (or not).
This means the order that routes are specified is important. The default route matches any URL with 0, 1, 2, or 3 segments. Therefore, in most cases you will need to define your custom route before the default route.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "IPA",
url: "IPA",
defaults: new { controller = "Account", action = "Login", IsIPA = "true" });
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
The above configuration will route http://myurl.com/IPA to the Controller named Account and Action method named Login, and pass the additional route key IsIPA. This same URL will be built for the Controller/Action/IsIPA combination because it is the first one that matches in the list.
Note that the original URL http://myurl.com/Account/Login/?IsIPA=true will still work and still route to the same location. This configuration just adds an extra route to that resource.
Without testing it, I think that you want this:
routes.MapRoute("IPA", "Account/Login/{IsIPA}",
new { controller = "Account", action = "Login", IsIPA = "true"});

Url routing not working as expected

This is my RegisterRoutes method in global.asax:
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 }
);
routes.MapRoute("ListBooks",
"Home/Books/{id}",
new { controller = "Home", action = "Books" },
new { id = #"\d{2}" });
}
As you can see in the constraint I have specified that the id should be compulsory there of 2 digits. But having specified this, even though I enter just a single digit book id it all still works out pretty well. Can anybody tell me what is wrong in this?
Your default route should be placed after the other routes, otherwise it would take the precedence.
Your constraint works as expected and url is not matched to the "ListBooks" route. But, if you look closer to the "Default" route, it has the same signature as the "ListBooks" one - but without constraint. So "Default" handles that single digit id url. In this case, your route order does not matter, as the "Default" will catch single digit id url anyways.
Try this
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("ListBooks",
"Home/Books/{id}",
new { controller = "Home", action = "Books" },
new { id = #"\d{2}" });
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}

Categories

Resources