将实例的属性附加到URL路径asp.net的路由配置

问题描述 投票:0回答:1

Global.asax:

   void Application_Start(object sender, EventArgs e)
        {
   public class Global : HttpApplication
    {
      GlobalConfiguration.Configure(config =>
            {
                config.MapHttpAttributeRoutes();

                RouteTable.Routes.MapHttpRoute(
                    name: "DefaultApi",
                    routeTemplate: "api/{controller}/{action}");
            });

}}

例如,我得到这个网址:

http://localhost:54404/books/20

我想要的是什么(在图书实例“ lawbook”的名称后面附加:):

http://localhost:54404/books/20/lawbook

asp.net url routes
1个回答
0
投票

Global.asax:

Global:HttpApplication内部

//add this
   public RouteValueDictionary SetRouteValueDefault(string name, string value = "")
        {
            return new RouteValueDictionary { { name, value } };
        }

        public void Register(RouteCollection routes)
        {

            routes.MapPageRoute("author", "books/{id}/{name}", "~/Books.aspx", false, SetRouteValueDefault("name"));
        }

Application_Start()内部:

//add this
            Register(RouteTable.Routes);

您现在可以根据需要组合实例的url

<a href="/books/book.id/book.name"></a>  

信用:Ali Kleit

© www.soinside.com 2019 - 2024. All rights reserved.