1.创建一个.NET MVC程序,在RouteConfig.cs里面注释掉传统路由,启用特性路由
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace WebApplication9
{
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.MapMvcAttributeRoutes();
}
}
}
2.此时如果在about操作上加上特性路由如下,则除了about视图通过http://localhost:59829/about可以访问意外,其他都无法访问
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace WebApplication9.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[Route("about")]
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
}
}
3 如果在index方法上添加特性路由 [Route("")], [Route("home")],[Route("home/index")]则Index视图通过http://localhost:59829,http://localhost:59829/home,http://localhost:59829/home/index都可以访问到
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace WebApplication9.Controllers
{
public class HomeController : Controller
{
[Route("")]
[Route("home")]
[Route("home/index")]
public ActionResult Index()
{
return View();
}
[Route("about")]
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
}
}
4.如果在每个方法上都加路由比较麻烦,可以在HomeController增加特性路由[Route("Home/{action}")],此路由就成为控制器的默认路由
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace WebApplication9.Controllers
{
[Route("Home/{action}")]
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
}
}
3.此时发现只能通过http://localhost:59829/home/index才能访问到主页,使用http://localhost:59829/home/about才能访问到about
4.如果想还想通过http://localhost:59829,http://localhost:59829/home访问到index,需要在index方法上重写默认路由
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace WebApplication9.Controllers
{
[Route("Home/{action}")]
public class HomeController : Controller
{
[Route("")]
[Route("home")]
[Route("home/index")]
public ActionResult Index()
{
return View();
}
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
}
}
5.为了解决默认路由和方法上的路由都有Home重复问题,可以将默认路由改成 [RoutePrefix("Home")], [Route("{action}")]这种写法,此时index的路由需要改为下面的写法,才能通过三种URL访问到,此时路由是由路由前缀和重写后的路由拼接组成
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace WebApplication9.Controllers
{
[RoutePrefix("Home")]
[Route("{action}")]
public class HomeController : Controller
{
[Route("~/")]
[Route("")]
[Route("index")]
public ActionResult Index()
{
return View();
}
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
}
}
6.对于同样的操作可以添加路由约束来区分开
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace WebApplication9.Controllers
{
[RoutePrefix("Person")]
[Route("{action}")]
public class PersonController : Controller
{
// GET: Person
public ActionResult Index()
{
return View();
}
[Route("{id:int}")]
public ActionResult Details(int id)
{
return View();
}
[Route("{name}")]
public ActionResult Details(string name)
{
return View();
}
}
}
7.通过使用默认值和可选参数可以简单高效的定义特性路由,例如 [Route("Person/{action=index}/{id?}")],可以实现通过
http://localhost:59829/person访问到index视图,通过http://localhost:59829/person/about访问到about视图,通过http://localhost:59829/person/details/1访问到detail视图,通过http://localhost:59829/person/update/1访问到update视图
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace WebApplication9.Controllers
{
[Route("Person/{action=index}/{id?}")]
public class PersonController : Controller
{
// GET: Person
public ActionResult Index()
{
return View();
}
public ActionResult Details(int id)
{
return View();
}
public ActionResult Update(int id)
{
return View();
}
public ActionResult About()
{
return View();
}
}
}