当我将多个参数传递给控制器操作时,会在参数中得到问号,如下所示:
http://localhost:57728/Home/AddAndManageProperties?BaseCategoryId=11&SubCategoryId=14
我想把问号去掉如下:
http://localhost:57728/Home/AddAndManageProperties/BaseCategoryId=11/SubCategoryId=14
这是我的代码:
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 }
);
}
}
以下是行动方法:
public ActionResult AddAndManageProperties(int? BaseCategoryId, int? SubCategoryId)
{
}
我通过这个方法调用方法AddAndManageProperties
[HttpPost]
public ActionResult AddSubCategory(SubCategory subCategory)
{
return RedirectToAction("AddAndManageProperties", new { BaseCategoryId = subCategory.BaseCategoryId, SubCategoryId = subCategory.SubCategoryId });
}
我是ASP.NET MVC的新手,所以请帮帮我!
发布于 2016-06-25 04:29:25
将MyRout
移动到Default
路由之前,并将其更改为
routes.MapRoute(
name: "MyRout",
url: "Home/AddAndManageProperties/{BaseCategoryId}/{SubCategoryId}",
defaults: new { controller = "Home", action = "AddAndManageProperties" }
);
请注意,只有最后一个参数可以标记为UrlParameter.Optional
,因此该方法需要
public ActionResult AddAndManageProperties(int BaseCategoryId, int SubCategoryId)
就上述路线而言,或
public ActionResult AddAndManageProperties(int BaseCategoryId, int? SubCategoryId)
如果将上述路由定义修改为
defaults: new { controller = "Home", action = "AddAndManageProperties", SubCategoryId = UrlParameter.Optional }
注意,如果您也希望在路由中包含文本"BaseCategoryId“和"SubCategoryId”,请使用
url: "Home/AddAndManageProperties/BaseCategoryId/{BaseCategoryId}/SubCategoryId/{SubCategoryId}",
发布于 2016-06-25 03:58:09
该问号用于查询字符串,并且它们是必需的,因为这是如何将数据分配给操作所期望的参数的。您不应该尝试删除它们,但是可以使用FromBody属性来发送查询字符串中的参数。
发布于 2016-06-25 04:32:10
首先,也是最重要的是,您的路由顺序是错误的,并且您有多个可能的URL,从而导致调用错误的路径。有关解释,请参见为什么在asp.net mvc中,在通用路由之前先映射特殊路由?。
第三,=
符号仅在查询字符串除非它被编码中有效。但是,海事组织,你应该在URL中不使用不安全字符,以避免所有的头痛伴随着他们。在这种情况下,另一种更好的选择是将=
替换为-
。
最后,如果您想真正地使参数是可选的,一种方法是提供多条路线,它允许某些路由中的参数,而不允许其他路由中的参数。
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 }
);
}
}
注意:如果您的参数是需要在中传递的,那么 斯蒂芬的回答也是一种很好的替代方法。海事组织,如果你的行动方法需要两个参数才能发挥作用,那么使用所需的参数就更有意义了。
但到目前为止,最简单的选择是简单地使用查询字符串。如果这样做,参数自然可以是可选的,并按任何顺序追加,而且除了Default
路由之外,不需要其他任何东西。
https://stackoverflow.com/questions/38028207
复制