NET Core的mvc服务彩票开奖网平台搭建和Route服务学习总结

mvc服务 和 route服务彩票开奖网平台搭建论坛:haozbbs.com Q1446595067
程序想要 增加 请求的路由服务,则需要 在ConfigureServices 中增加路由服务,如下
services.AddRouting();
1
并且在 Configure 中配置路由并使用,示例如下:

var trackPackageRouteHandler = new RouteHandler(context =>
{
return context.Response.WriteAsync("33333");
});

        var routeBuilder = new RouteBuilder(app, trackPackageRouteHandler);

        routeBuilder.MapRoute(
            "Track Package Route",
            "package/{operation:regex(^(track|create|detonate)$)}/{id:int}");
        var routes = routeBuilder.Build();

        app.UseRouter(routes);

1
2
3
4
5
6
7
8
9
10
11
12
13
对于mvc服务,当增加一个 mvc服务时,这其中默认包含并开启了 路由服务,也就是说 当使用 services.AddMvc();时,可以不用 services.AddRouting(); 依然可以在 Configure中配置并使用路由服务(虽然这是没有必要的)。
示例代码:
public void ConfigureServices(IServiceCollection services)
{
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)
{
var trackPackageRouteHandler = new RouteHandler(context =>
{
return context.Response.WriteAsync("33333");
});

        var routeBuilder = new RouteBuilder(app, trackPackageRouteHandler);

        routeBuilder.MapRoute(
            "Track Package Route",
            "package/{operation:regex(^(track|create|detonate)$)}/{id:int}");
        var routes = routeBuilder.Build();

        app.UseRouter(routes);

        app.UseMvc();
    }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
mvc服务开启并使用时,系统会 自动找到所有 继承了 Controller的类,并查看他的路由特性,如果该类配置了路由特性的话,程序会自动实例化一个对象,并将相应的路由配置增加到 Route 上。
运行结果:我们可以看到访问相应设置的路由,数据成功返回
这里写图片描述
示例代码
对于以下demo我们访问 /value/test借口 就会收到 “test”的提示文本

Startup.cs 文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Internal;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;

namespace Configuration
{
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)
{
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)
    {   
        app.UseMvc();
    }
}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
ValueController.cs文件
using System;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace Configuration.Controllers
{
[Route("[controller]")]
public class ValueController: Controller
{
public ILogger _logger;
public ValueController(ILogger<Book> logger)
{
_logger = logger;
}

    [HttpGet("Test")]
    public string Test()
    {
        Console.WriteLine("333333333333333333");
        _logger.LogInformation("ddddddddddddd");
        return "test";
    }
}

public class Book
{
    public string name { get; set; }
    public string price { get; set; }
}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
运行结果 (我们可以看到访问Controller中的借口时,数据成功返回)
这里写图片描述

猜你喜欢

转载自blog.51cto.com/13855531/2137027
今日推荐