.NetCore -MVC 路由的配置

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
//参数验证,路由的参数验证配置
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Routing.Constraints;

namespace Han.oi.Web
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            //引入MVC模块
            services.AddMvc();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "Default",
                    template: "{controller}/{action}",
                    defaults: new { controller = "Home", action = "index" }
                );
                routes.MapRoute(
                    name: "Han1",
                    template: "{controller}/{action}",
                    defaults: new { controller = "Han1", action = "Time"}
                );

               /*  routes.MapRoute(
                   name: "Tutorial",
                   template: "{controller}/{action}/{name}/{age?}",
                   defaults: new { controller = "Tutorial"}
                ); */

               /*  routes.MapRoute(
                    name: "Tutorial",
                    template: "{controller}/{action}/{age}/{name}",
                    defaults: new { controller = "Tutorial",action="Welcome",name = "韩"},
                    constraints: new { name = new MaxLengthRouteConstraint(5) }
                ); */

                routes.MapRoute(
                   name: "Tutorial_1",
                   template: "{controller}/{action}/{age:range(1,150)}/{name:maxlength(5)}",
                   defaults: new { controller = "Tutorial", action = "Welcome", name = "韩" }
                );

                /* routes.MapRoute(
                   name: "Tutorial_1",
                   template: "hello/{action}/{age:range(1,150)}/{name:maxlength(5)}",
                   defaults: new { controller = "Tutorial", action = "Welcome", name = "韩" }
                ); */

                routes.MapRoute(
                   name: "jiaocheng_1",
                   template: "jioachen/{action}.html"
                );
            });

        }
    }
}

原文地址:https://www.cnblogs.com/han-guang-xue/p/10559265.html

时间: 2024-08-12 07:45:30

.NetCore -MVC 路由的配置的相关文章

ASP.NET Core中使用默认MVC路由

ASP.NET Core里Route这块的改动不大,只是一些用法上有了调整,提供了一些更加简洁的语法. 而对于自定义路由的支持当然也是没有问题的,这个功能应该是从MVC1.0版本就已经有这个功能. 先看看ASP.NET Core里面实现默认MVC路由的配置方式 通常情况下,在使用MVC项目的时候,默认的路由就足够了,就是常见的通过Controller和Action获取具体的方法的方式. 从一个最基本的项目开始,执行以下步骤,就可以使得项目支持MVC路由 1.创建一个空白的ASP.NET Core

MVC路由自定义及视图找寻规则

这篇关于MVC路由及视图规则本来是昨天要发的,但是本人真的有点懒,终于今天忍无可忍了.初学MVC的时候比现在还菜一点(现在也很菜),想着会用就行,但是有时还是会好奇,为什么它能找到控制器?为什么控制器return View();就能找到视图,而为什么视图一定要建在Views文件下?好像说的有点多了,接下来一边上例子,一边分析! MVC路由自定义 相信对于MVC路由的配置大家也都了解过一些,其实,这也不是本章的重点. 创建MVC项目的时候,根目录下>>App_Start>>Route

.NetCore MVC中的路由(2)在路由中使用约束

.NetCore MVC中的路由(2)在路由中使用约束 0x00 路由模板中的约束 在配置路由模板时,除了以静态字符和变量的形式外,还可以通过配置对变量进行约束.实际如果不嫌麻烦的话在路由到的Action中对变量进行检查也是一种方法,不过对于变量的通用的约束当然是放在路由层面更加合适.而且这样做会简化Action的代码,使Action更加专注于自身业务,符合AOP的思路.这篇文章主要介绍路由模板中的约束. 0x01 在路由模板中使用约束 在路由模板中使用约束是很简单的,如下所示: {contro

史上最全的ASP.NET MVC路由配置,以后RouteConfig再弄不懂神仙都难救你啦~

继续延续坑爹标题系列.其实只是把apress.pro.asp.net.mvc.4.framework里的CHAPTER 13翻译过来罢了,当做自己总结吧.内容看看就好,排版就不要吐槽了,反正我知道你也不会反对的. XD 首先说URL的构造. 其实这个也谈不上构造,只是语法特性吧. 命名参数规范+匿名对象 routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new

MVC路由配置,伪静态。

前段时间,研究了一下mvc路由配置伪静态,在网上扒了很多最后还是行不通,所以我现在把这些心得整理出来,供大家分享: 1.mvc中默认路由配置是:http://localhost:24409/Home/Index routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = &quo

MVC路由机制(转)

今天我来缕一下MVC的路由机制,因为使用MVC已经快一年了,之前也只是上手,没有系统去理会.项目完了,结合实际使用,回过头来深入一下. MVC 学习索引(点击即可) 一个请求进入IIS后 传统ASP.NET 路由部分 1.IIS根据文件类型将请求转交给相应的处理程序,如果属于ASP.NET文件,则将请求转发给aspnet_isapi.dll.(注:在II6和IIS7上的处理方式是有区别的) 2. HTTP处理模块UrlRoutingModule接收到请求后,循环调用其RouteCollectio

ASP.NET MVC 路由进阶(之二)--自定义路由约束

3.自定义路由约束 什么叫自定义路由约束呢?假如路由格式为archive/{year}/{month}/{day},其中year,month,day是有约束条件的,必须是数字,而且有一定范围. 这时候,我们就可以设置约束类,进行自定义路由约束了. 第一步: 我们先添加年份限制类 YearRouteConstraint.请看代码: using System; using System.Collections.Generic; using System.Linq; using System.Web;

Asp.Net MVC 路由 【转】

原文链接:http://www.asp.net/learn/mvc/ 在这篇教程中,我将为你介绍每个ASP.NET MVC应用程序都具有的一个重要功能,称作ASP.NET路由(ASP.NET Routing).ASP.NET路由模块负责将即将到来的浏览器请求映射到特定的MVC控制器动作.学完这篇教程之后,你将会理解标准的路由表是如何将请求映射到控制器动作的. 1. 理解默认路由表 当你创建一个新的ASP.NET MVC应用程序时,应用程序已经被配置为使用ASP.NET路由.ASP.NET路由在两

【ASP.NET MVC 牛刀小试】 ASP.NET MVC 路由

例子引入 先看看如下例子,你能完全明白吗? 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.Mvc; 6 using System.Web.Routing; 7 8 namespace MVCDemo 9 { 10 public class RouteConfig 11 { 12 public static void Re