特性路由

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:59829http://localhost:59829/homehttp://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:59829http://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();
        }

    }
}

 

特性路由、中间件路由和端点路由都是在构建Web应用或API时,用于处理请求的路由机制。它们通常出现在像.NET Core这样的现代Web框架中,允许开发者定义如何将HTTP请求映射到具体的处理程序。下面是对它们的简要介绍: 特性路由(Attribute Routing): 特性路由是通过在控制器的动作方法上使用特定的路由属性来定义路由的方式。这种方式将路由信息直接注释到控制器的类或方法上,使路由定义与应用程序的业务逻辑更加紧密地结合在一起。这样做的好处是可以在不改变URL结构的情况下,轻松地对控制器和动作进行组织和重构。 中间件路由(Middleware Routing): 中间件路由是指在请求处理管道中使用路由来决定是否继续处理请求,或者将其传递给下一个中间件组件。它通常在中间件组件中被配置,用于在请求到达应用的核心业务逻辑之前进行拦截和处理。中间件路由是处理请求的另一个层次,提供了对请求处理流程更细粒度的控制。 端点路由(Endpoint Routing): 端点路由是一种更加灵活和可扩展的路由机制,它将每个路由映射到一个端点(Endpoint),并允许在路由配置中包含更多的数据和行为。端点路由通常用于处理复杂的应用场景,比如基于请求的不同属性来动态地选择处理程序。这种方式不仅允许对路由进行精细的控制,还支持异步编程和中间件链的扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值