【问题标题】:how to Create Url in mvc4如何在 mvc4 中创建 URL
【发布时间】:2023-03-06 10:34:01
【问题描述】:

自定义路线在我的情况下不起作用

我正在尝试在 mvc4 中创建一个 URL。

路由网址:-

routes.MapRoute(
                name: "DocumentDetails",
                url: "DocumentDetails",
            defaults: new { Controller = "Home", action = "DocumentDetails", DocumentId = UrlParameter.Optional }
         );

控制器:-

 public void DocumentDetails(int DocumentId)
        {
           some code
        }

我期待以下网址。 只是我试图在 url 中删除我的控制器名称。

www.mysite.com/DocumentDetails?DocumentId=15

当前工作网址

www.mysite.com/Home/DocumentDetails?DocumentId=15

【问题讨论】:

    标签: asp.net-mvc-4 asp.net-mvc-routing


    【解决方案1】:

    您可以使用 MapMvcAttributeRoutes。 将此添加到您的控制器中

    [Route("DocumentDetails/{DocumentId}")]
    public ActionResult DocumentDetails(int DocumentId)
        {
           some code
        }
    

    现在将 documentid 设置为 url。 您需要将其添加到您的 RouteConfig 中

    routes.MapMvcAttributeRoutes();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-12
      • 2012-10-05
      • 1970-01-01
      相关资源
      最近更新 更多