1. 程式人生 > >NET Core的mvc服務彩票開獎網平臺搭建和Route服務學習總結

NET Core的mvc服務彩票開獎網平臺搭建和Route服務學習總結

依然 pen environ fig 數據 builder 特性 ets space

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中的借口時,數據成功返回)
這裏寫圖片描述

NET Core的mvc服務彩票開獎網平臺搭建和Route服務學習總結