如何删除Asp.net中的问号MVC路由多个参数

时间:2016-06-25 11:51:53

标签: c# asp.net-mvc

当我将多个参数传递给控制器​​动作时,我在参数中得到问号:

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的新手,所以请帮助我!

3 个答案:

答案 0 :(得分:0)

该问号用于查询字符串,并且它们是必需的,因为这是将数据分配给您的操作所期望的参数的方式。您不应该尝试删除它们,但可以使用[FromBody]属性,而不是在查询字符串中发送参数。

答案 1 :(得分:0)

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}",

答案 2 :(得分:0)

首先,最重要的是,您的路由顺序错误,并且您有多个可能的URL导致调用错误的路由。有关说明,请参阅Why map special routes first before common routes in asp.net mvc

其次,routes cannot contain more than one UrlParamter.Optional

第三,=符号仅在查询字符串unless it is encoded中有效。但是IMO,你应该not use unsafe characters in a URL来避免随之而来的所有麻烦。在这种情况下,更好的选择是将=替换为-

最后,如果你想真正使参数成为可选的,一种方法是provide multiple routes允许某些路线中的参数而不是其他路线。

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 }
        );
    }
}
  

注意:如果您的参数必需要在网址中传递, Stephen's answer也是此方法的一个很好的替代方法。 IMO,如果你的动作方法需要它们两个才能运行,那么使用必需参数会更有意义。

但到目前为止,最简单的选择是简单地使用查询字符串。这些参数当然可以是可选的,如果您这样做,也可以按任何顺序附加,并且您不需要比Default路线更多的内容。