我有像
这样的动作 public class EmployerController : Controller
{
public ActionResult Message(string contractorId, int projectId)
{....}
和@ html.ActionLink为
@Html.ActionLink("Message", "Message", "Employer", new { contractorId = @model.ContractorId, projectId = @model.ProjectId }, null)
我最终得到像
这样的网址http://localhost:3597/Employer/Message?contractorId=contractor&projectId=10
而我想要像
这样的网址http://localhost:3597/Employer/Message/contractor/10
在我的RouteConfig.cs中,我添加了
routes.MapRoute(
name: "MessageRoute",
url: "Employer/Message/{contractorId}/{projectId}",
defaults: new { controller = "Employer", action = "Message", contractorId = (string)null, projectId = 0 }
);
我错过了什么。如何在路由中禁用查询字符串?
/感谢。
答案 0 :(得分:0)
在你的RouteConfig.cs中,你是否只有你在帖子中提到的路线(“MessageRoute”),或者你也有默认路线(下面)?我怀疑你可能在“MessageRoute”之前注册了以下默认路由,这就是你看到这种行为的原因。
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
如果是,您应该按照这些路线的注册顺序。
答案 1 :(得分:0)
您可以按路径网址生成网址
<a href='@Url.RouteUrl("MessageRoute", new { contractorId = 6, projectId = 5})'>link</a>
和你的路线
routes.MapRoute(
name: "MessageRoute",
url: "Employer/Message/{contractorId}/{projectId}",
defaults: new { controller = "Employer", action = "Message", contractorId = UrlParameter.Optional, projectId = UrlParameter.Optional }
);
对我有用,结果网址是
/Employer/Message/6/5
或者如果你需要url喜欢/ Employer / Message / contraktor / 6 / project / 5只需改变路线
routes.MapRoute(
name: "MessageRoute",
url: "Employer/Message/contraktor/{contractorId}/project/{projectId}",
defaults: new { controller = "Employer", action = "Message", contractorId = UrlParameter.Optional, projectId = UrlParameter.Optional }
);