【问题标题】:How to remove question marks in Asp.net MVC routes multiple parameters如何在 Asp.net MVC 中删除问号路由多个参数
【发布时间】:2016-06-25 20:43:38
【问题描述】:

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

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

【问题讨论】:

  • 首先您需要删除BaseCategoryId = UrlParameter.Optional, SubCategoryId = UrlParameter.Optional - 只有最后一个参数可以是可选的。然后,如果您将其更改为 url: "Home/AddAndManageProperties/{BaseCategoryId}/{SubCategoryId}", 并将其移动到默认路由之前,假设您的方法具有匹配的参数名称,它将起作用。
  • @StephenMuecke 非常感谢您的帮助 :) 能否请您写下代码,以便我能理解...对不起,我是 mvc 的新手。谢谢
  • 首先编辑您的问题以包含您的AddAndManageProperties() 方法的签名,并显示您如何生成网址(我假设您使用@Html.ActionLink()?
  • 你是如何生成 url - 使用 @Html.ActionLink() 还是 <form>(使用 FormMethod.Get
  • @StephenMuecke 我再次编辑问题。

标签: c# asp.net-mvc


【解决方案1】:

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

【讨论】:

  • 当然 - 只需将其更改为 url: "Home/AddAndManageProperties/BaseCategory/{BaseCategoryId}/SubCategory/{SubCategoryId}", (当您将 url 放入 cmets 时,用反引号将其包围 :)
  • 谢谢,但我试过了,但不起作用 url:“Home/AddAndManageProperties/BaseCategory/{BaseCategoryId}/SubCategory/{SubCateg‌​oryId}”,默认值:new { controller = "Home",动作 = "AddAndManageProperties" }
  • 是的,现在它正在工作 :) 非常感谢斯蒂芬 :) 上帝保佑你 :)
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    首先,也是最重要的,您的路由顺序错误,并且您有多个可能的 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 也是此方法的一个很好的替代方案,如果您的参数需要在 URL 中传递。 IMO,如果您的操作方法需要两个参数才能起作用,则使用必需参数更有意义。

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

    【讨论】:

    • 非常感谢 :) 我现在才看到您的回答.. 它也有效。上帝保佑你:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多