When I pass multiple parameters to a controller action, I get question marks in the parameters like this:
http://localhost:57728/Home/AddAndManageProperties?BaseCategoryId=11&SubCategoryId=14
I want to remove the question marks to be like this:
http://localhost:57728/Home/AddAndManageProperties/BaseCategoryId=11/SubCategoryId=14
here is my code:
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 }
);
routes.MapRoute(
name: "MyRout",
url: "{controller}/{action}/{BaseCategoryId}/{SubCategoryId}",
defaults: new { controller = "Home", action = "AddAndManageProperties", BaseCategoryId = UrlParameter.Optional, SubCategoryId = UrlParameter.Optional }
);
}
}
And here is the Action Method:
public ActionResult AddAndManageProperties(int? BaseCategoryId, int? SubCategoryId)
{
}
And i call the method AddAndManageProperties by this method
[HttpPost]
public ActionResult AddSubCategory(SubCategory subCategory)
{
return RedirectToAction("AddAndManageProperties", new { BaseCategoryId = subCategory.BaseCategoryId, SubCategoryId = subCategory.SubCategoryId });
}
I am new to ASP.NET MVC so please help me!
Move the MyRout to be before the Default route and change it to
routes.MapRoute(
name: "MyRout",
url: "Home/AddAndManageProperties/{BaseCategoryId}/{SubCategoryId}",
defaults: new { controller = "Home", action = "AddAndManageProperties" }
);
Note that only the last parameter can be marked UrlParameter.Optional so the method needs to be
public ActionResult AddAndManageProperties(int BaseCategoryId, int SubCategoryId)
for the above route, or
public ActionResult AddAndManageProperties(int BaseCategoryId, int? SubCategoryId)
if you modify the above route definition to
defaults: new { controller = "Home", action = "AddAndManageProperties", SubCategoryId = UrlParameter.Optional }
Note, if you also want to include the text "BaseCategoryId" and "SubCategoryId" in the route, use
url: "Home/AddAndManageProperties/BaseCategoryId/{BaseCategoryId}/SubCategoryId/{SubCategoryId}",
That question marks are used for query strings, and they are required because this is how data is assigned to parameters your actions expect. You should not try to remove them, but you could use the [FromBody] attribute, not to send the parameters in query string.
Firstly, and most importantly, your routes are in the wrong order and you have multiple possible URLs that result in calling the wrong route. See Why map special routes first before common routes in asp.net mvc for an explanation.
Secondly, routes cannot contain more than one UrlParamter.Optional.
Third, the = sign is only valid within a query string, unless it is encoded. But IMO, you should not use unsafe characters in a URL to avoid all of the headaches that come with them. A better alternative in this case would be to replace the = with a -.
Finally, if you want to truly make the parameters optional, one approach is to provide multiple routes that allow the parameters in certain routes but not others.
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "BaseCategoryAndSubCategoryId",
url: "{controller}/{action}/BaseCategoryId-{BaseCategoryId}/SubCategoryId-{SubCategoryId}",
defaults: new { controller = "Home", action = "AddAndManageProperties" }
);
routes.MapRoute(
name: "BaseCategoryIdOnly",
url: "{controller}/{action}/BaseCategoryId-{BaseCategoryId}",
defaults: new { controller = "Home", action = "AddAndManageProperties" }
);
routes.MapRoute(
name: "SubCategoryIdOnly",
url: "{controller}/{action}/SubCategoryId-{SubCategoryId}",
defaults: new { controller = "Home", action = "AddAndManageProperties" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
NOTE: Stephen's answer is also a good alternative to this approach if your parameters are required to be passed in the URL. IMO, it makes more sense to use required parameters if your action method requires both of them in order to function.
But by far the simplest option is to simply use the query string. The parameters can naturally be optional and appended in any order if you do it that way, and you don't need anything more than your Default route.
Related
I am having an issue while dealing with routing in MVC.
I have defined the following routes in Route.Config
routes.MapRoute(
name: "Test",
url: "{controller}/{action}/{param}",
defaults: new { controller = "Home", action = "FirstAction" }
);
routes.MapRoute(
name: "Testy",
url: "{controller}/{action}/{secondparm}",
defaults: new { controller = "Home", action = "SecondAction" }
);
routes.MapRoute(
name: "Test2",
url: "{controller}/{action}/{encodedparam}",
defaults: new { controller = "User", action = "UserInfo" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Problem is that the first route is working fine but in second and third I got null values for the respective parameter.
Did I miss something?
Thanks in advance
You must match the parameter name for the third. If you write :
{id}
You must write
public ActionResult AnyAction(int id)
id could be of any type
i think there is conflict beetween your routes. You need only this mapping :
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
if you need custom routing for other elements don't put "{controller}/{action}/..." inside, because he will take the first route config that is matching with.
You can call all your routes precising the name of your parameter if its different from "id" :
http://localhost/home/firstaction?param=123
http://localhost/home/secondaction?secondparam=123
http://localhost/user/userinfo?encodedparam=123
As you want to use the URL in such way: localhost:portnumber/home/secondaction/value instead of localhost:portnumber/home/secondaction?secondparam=value.
You have to follow the below approach, which would behave as generic for all your Action Methods, whether they are containing parameters or not.
Declare default route in RouterConfig.cs as:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Then Parameter name in method must be same as in default route:
public ActionResult SecondAction(string id)
{
return View();
}
Paramter type could be any type.
In this way you do not need to declare other routes.
I couldn't get any good information on how to define a route to a contrete action in the selected controller. MSDN doesn't provide clean information on this. There is a mention of action parameter but it doesn't seem to be working.
What I want to achive is to route path like /vehicles/check*** to Check method in the VehiclesController.
A concrete invokation is /vehicles/check?licencePlate=XYZ =>
VehiclesController -> Check(string licenePlate)
I have this map but it does not work:
config.Routes.MapHttpRoute("VehicleTransactions", "Vehicles/Check",
new { controller = "Vehicles", action= "Check" });
Can it be done with MVC?
Thanks, Radek
MapHttpRoute is used for mapping Web API routes. For MVC Controller routes we tend to use MapRoute
So Assuming
public class VehiclesController : Controller {
public ActionResult Check(string licenePlate) {
//...
return View();
}
}
The route would be mapped to
routes.MapRoute(
name: "VehicleTransactions",
url: "vehicles/check",
defaults: new { controller = "Vehicles", action= "Check" }
);
Try this:
routes.MapRoute(
name: "VehicleTransactions",
url: "Vehicles/Check/{licencePlate}",
defaults: new { controller = "Vehicles", action = "Check", licencePlate = UrlParameter.Optional });
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });
Set "VehicleTransactions" routing before of "Default" routing, because otherwise "Default" overwrite the other.
Special routes is set always before default routing.
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 }
);
I've added new action to my asp.net mvc application and add specific rule for it inside RouteConfig.cs.
But all parameters passed as null.
Here is my route rule:
routes.MapRoute(
"toekn_submit_route",
"{controller}/SendToken/{platform}/{token}/{uid}",
new { controller = "Home", action = "SendToken" }
, new[] { "MvcApplication.Controllers" }
);
And here is action deceleration:
public JsonResult SendToken(string platform, string token, string uid) { ... }
I call action using this URL: http://localhost:51650/Home/SendToken/platform/token/uid
Order of how routes are added is important. First matched route wins.
Make sure that this added route is added before more general routes otherwise they would be matched by another route that doesn't populate the placeholders as intended.
routes.MapRoute(
name: "token_submit_route",
url: "{controller}/SendToken/{platform}/{token}/{uid}",
defaults: new { controller = "Home", action = "SendToken" },
namespaces: new[] { "MvcApplication.Controllers" }
);
//...other more general routes.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
For example if the Default route was placed before the token route it would still match http://localhost:51650/Home/SendToken/platform/token/uid
where
controller = "Home",
action = "SendToken",
id = "platform/token/uid"
I have the following controller:
public class MyController : BaseController
{
public ActionResult Index(string id) { /* Code */ }
public ActionResult MyAjaxCall(string someParameter) { /* Code */ }
}
I have also added the following in the RouteConfig.cs
routes.MapRoute(
name: "MyController",
url: "MyController/{id}",
defaults: new { controller = "MyController", action = "Index" }
)
So my idea is to be able to go directly to the index action using this url /MyController/{Id}, and that seems to work.
However when on the Index page I need to make an Ajax call to /MyController/MyAjaxCall/{someParameter}. However this url is pointing to the Index controller, and is interpreting MyAjaxCall as the id in the Index action.
Any ideas how I can exclude this action from following the newly added route config setting?
If that your id can only be integer number, you can add a constraint to your id field, which specifies that your id can only be numbers:
routes.MapRoute(
name: "MyController",
url: "MyController/{id}",
defaults: new { controller = "MyController", action = "Index" },
constraints: new { id = #"\d+" } // <- constraints of your parameters
)
Here you can use any regular expression that works for your business logic.
Also make sure to register this route before your default route registration, in that case MVC will first try to match this route, and only if it doesn't match it will try to match the default route.
It sounds like you have the routes in the wrong order. When using MVC routing, the first match always wins, so you must place the most specific routes first before general routes.
routes.MapRoute(
name: "MyControllerAJAX",
url: "MyController/MyAjaxCall/{someParameter}",
defaults: new { controller = "MyController", action = "MyAjaxCall" }
)
routes.MapRoute(
name: "MyController",
url: "MyController/{id}",
defaults: new { controller = "MyController", action = "Index" }
)
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);