【问题标题】:Removing Action name from URL and add page title to URL - Asp.net MVC从 URL 中删除操作名称并将页面标题添加到 URL - Asp.net MVC
【发布时间】:2016-12-28 08:44:35
【问题描述】:

我有一个这样的网址:

http://localhost:17594/Contact/Contact

现在我想这样显示:

http://localhost:17594/Contact/Contact-us

路由配置:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        name: "Categories",
        url: "Categories/{id}",
        defaults: new { controller = "Categories", action = "Index", id = UrlParameter.Optional },
        namespaces: new[] { "FinalKaminet.Controllers" }
    );

    routes.MapRoute(
        name: "Contacts",
        url: "{controller}/{title}",
        defaults: new { controller = "Contact", action = "Contact", title = UrlParameter.Optional },
        namespaces: new[] { "FinalKaminet.Controllers" }
    );

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

}

查看

@Html.ActionLink("Contact Us", "Contact" , "Contact" , new { title = "contact-us" } , null)

但我在使用Categories 地图路线的第 63 行遇到错误。

异常详情:System.InvalidOperationException: No route in the 路由表与提供的值匹配。

来源错误:

第 62 行:@Html.ActionLink("وبلاگ", "")

第 63 行:@Html.Action("MenuCat" , "Home")

怎么了?

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-routing url-routing


    【解决方案1】:

    你有两个选择。

    通过基于约定的路由添加特定路由

    routes.MapRoute(
        name: "ContactUs",
        url: "contact/contact-us",
        defaults: new { controller = "Contact", action = "Contact" },
        namespaces: new[] { "FinalKaminet.Controllers" }
    );
    
    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}/{title}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional , title = UrlParameter.Optional },
        namespaces: new[] { "FinalKaminet.Controllers" }
    );
    

    或者在基于约定的路由之前在 RouteConfig 中启用属性路由

    //enable attribute routing
    routes.MapMvcAttributeRoutes();
    
    //other convention-based routes.
    routes.MapRoute(....);
    

    并将路由直接应用到控制器和动作。

    public class ContactController : Controller {
    
        //GET contact/contact-us
        [HttpGet]
        [Route("Contact/Contact-us")]
        public ActionResult Contact() { … }
    
    }
    

    【讨论】:

      【解决方案2】:

      要将 url 操作显示为 Contact-us(定义路由),您可以使用 Attribute Routing。

      [Route("Contact/Contact-us")]
          public ActionResult Contact() { … }
      

      有关详细信息,请参阅 msdn。 https://blogs.msdn.microsoft.com/webdev/2013/10/17/attribute-routing-in-asp-net-mvc-5/

      【讨论】:

        【解决方案3】:

        试试这个

        在您的 路由配置 文件中:

        routes.MapRoute(
                name: "Contacts",
                url: "Contact/{action}/{title}",
                defaults: new { controller = "Contact", action = "Contact", title = UrlParameter.Optional },
                namespaces: new[] { "FinalKaminet.Controllers" }
            );
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-09-07
          • 2020-09-25
          • 2013-09-16
          • 1970-01-01
          • 2020-09-09
          相关资源
          最近更新 更多