在 ASP.NET MVC 应用程序中,它是更常见的做法在作为路由数据 (像我们一样与身份证上面) 比将它们作为查询字符串传递的参数中传递。

public string Welcome(string name, int ID = 1)
{
    return HttpUtility.HtmlEncode("Hello " + name + ", ID: " + ID);
}

 

 

您也可以添加一条路线来传递name numtimes 作为路由数据的 URL 中的参数。App_Start\RouteConfig.cs文件中,添加"Hello"路由:

 

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

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

      routes.MapRoute(
           name: "Hello",
           url: "{controller}/{action}/{name}/{id}"
       );
   }
}

 

运行该应用程序,浏览至 /localhost:XXX/HelloWorld/Welcome/Scott/3.

MVC中路由的修改和浏览器的地址参数

对于许多的 MVC 应用程序,默认路由工作正常。您将学习以后在本教程中,通过使用模型联编程序,数据和你不必修改默认路由的。

 

相关文章:

  • 2022-03-09
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-03
  • 2021-12-02
  • 2022-02-08
猜你喜欢
  • 2021-09-26
  • 2022-12-23
  • 2021-12-12
  • 2022-12-23
  • 2021-07-07
  • 2022-02-08
相关资源
相似解决方案