RouteConfig.cs 代码如下:

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

            //自定义路由标签
            routes.MapMvcAttributeRoutes();
           
            //默认路由
            //routes.MapRoute(
            //    name: "Default",
            //    url: "{controller}/{action}/{id}",
            //    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            //    namespaces: new string[] { "WebTest.Controllers" }
            //);
        }
    }

 

Controller自定义路由标签:

 [RoutePrefix("Test")]
    public class ProductController : Controller
    {
        
        [HttpGet,Route("Index")]
        public ActionResult Index(int? pageIndex=1,int? pageSize=8)
        {
            ProductService service = new ProductService();
            int index = Convert.ToInt32(pageIndex);
            int size = Convert.ToInt32(pageSize);
            var list = service.GetList(index, size);
            ViewBag.products = list;
            return View();
        }

        [HttpGet,Route("Demo")]
        public ActionResult One()
        {
            List<UserModel> list = new List<UserModel>();
            for (int i = 1; i < 10; i++)
            {
                list.Add(new UserModel()
                {
                    Id = i,
                    Name = "test"+i,
                    Password = "123456"
                });
            }
            ViewBag.Users = list;
            return View();
        }
    }

 

相关文章:

  • 2022-12-23
  • 2021-10-20
  • 2021-08-12
  • 2021-07-20
  • 2021-07-08
  • 2021-08-11
  • 2021-08-15
猜你喜欢
  • 2022-12-23
  • 2021-06-19
  • 2022-12-23
  • 2022-02-13
  • 2022-12-23
  • 2021-12-26
  • 2022-12-23
相关资源
相似解决方案