码迷,mamicode.com
首页 > Web开发 > 详细

.NetCore -MVC 路由的配置

时间:2019-03-19 16:37:17      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:control   mvc   app   nbu   request   form   led   soft   tar   

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"
                );
            });


        }
    }
}

 

.NetCore -MVC 路由的配置

标签:control   mvc   app   nbu   request   form   led   soft   tar   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!