【发布时间】: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