3

我有一个动作

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

我错过了什么。如何禁用路由中的查询字符串?

/谢谢。

4

2 回答 2

0

在您的 RouteConfig.cs 中,您是否只有上面在帖子中提到的路由(“MessageRoute”),或者您是否也有默认路由(如下)?我怀疑您可能在“MessageRoute”之前注册了以下默认路由,这就是您看到此行为的原因。

routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

如果是,您应该颠倒这些路线的注册顺序。

于 2013-06-07T22:08:23.747 回答
0

您可以像这样通过路由 url 生成 Url

<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 

或者如果你需要像 /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 }
   );
于 2013-06-08T12:30:14.300 回答