GithubHelp home page GithubHelp logo

route's Introduction

Route


Attribute Routing

加入 [Route] 到 Controller:

[Route("books")]
[ApiController]
public class BooksController : ControllerBase
{
    // GET: /Books
    [HttpGet]
    [Route("")]
    public async Task<ActionResult<IEnumerable<Book>>> GetBook() { ... }
    
    // GET: /Books/5
    [HttpGet()]
    [Route("{id:int}")]
    public async Task<ActionResult<Book>> GetBook(int id) { ... }
    
    // 使用Router才能支援 RESTful API 中常見的某些 URI 模式
    // 查詢特定 Author
    // GET: authors/{authorId}/books
    [HttpGet]
    [Route("~/authors/{authorId:int}/books")]
    public IActionResult GetBooksByAuthor(int authorId) { ... }
    
    // GET: /books/date/2016-06-27
    [HttpGet]
    //[Route("date/{publishDate:datetime}")]
    //[Route("date/{publishDate:datetime:regex(\d{{4}}-\d{{2}}-\d{{2}})}")]
    [Route(@"date/{publishDate:datetime:regex(\d{{4}}-\d{{2}}-\d{{2}})}")]
    public IActionResult Get(DateTime publishdate) { ... }
}

使用 [RoutePrefix] 屬性為整個控制器設定公共前置前置字串:

[RoutePrefix("api/books")]
public class BooksController : ApiController
{
    // GET api/books
    [Route("")]
    public IEnumerable<Book> Get() { ... }

    // GET api/books/5
    [Route("{id:int}")]
    public Book Get(int id) { ... }

    // POST api/books
    [Route("")]
    public HttpResponseMessage Post(Book book) { ... }
}

Map動詞()

  • 判斷請求來源的資訊(例如瀏覽器)
    app.Use((context, next) =>
    {
        var userAgent = context.Request.Headers["User-Agent"].ToString();
        //只允許Postman通過
        if (userAgent.Contains("Postman"))
        {
         return next();
        }
        else
        {
         return context.Response.WriteAsync("Error");
        }
    });
  • 建立 Middleware 類別
    • 把自製的 Middleware 邏輯獨立出來。 自製的Middleware中要有一個非同步的 Invoke 方法。
    public class SampleMiddleware
    {
        private readonly RequestDelegate _next;
    
        public SampleMiddleware(RequestDelegate next)
        {
            _next = next;
        }
    
        public async Task Invoke(HttpContext context)
        {
            var userAgent = context.Request.Headers["User-Agent"].ToString();
            if (userAgent.Contains("Postman"))
            {
                await _next(context);
            }
            else
            {
                await context.Response.WriteAsync("Error");
            }
        }
    }
  • 註冊到Startup的Configure
    app.UseMiddleware<BrowserMiddleware>();

參考資料:
精準解析 ASP.NET Core Web API
ASP.NET Web API 中的路由
ASP.NET Web API 中的路由和動作選取
ASP.NET Web API 2 中的屬性路由 [鐵人賽 Day03] ASP.NET Core 2 系列 - Middleware


  • 如果使用空白專案,要在Startup.cs新增

    • Configuration

      public IConfiguration Configuration { get; }
      
      public Startup(IConfiguration configuration)
      {
          Configuration = configuration;
      }
    • ServiceCollection

      public void ConfigureServices(IServiceCollection services)
      {
          services.AddControllers();
      }
    • endpoints.MapControllers();

      public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
      {
          app.UseEndpoints(endpoints =>
          {
              endpoints.MapControllers();
          });
      }
  • 新增 Web API Controller
    於 Add Scaffold → 選擇:Web API 2 Controller with actions, using Entity Framework

    • Model class: Book
    • Controller name: BooksController
    • Data context class: 選擇 + 號:BookSampleContext
  • 加入SeedDate

    • 開啟 Package Manager Console,輸入以下指令:
      PM> Add-Migration initial
    • 在 DbContext 衍生類別覆寫 OnModelCreating(),加入SeedData。
    • 輸入以下指令,Migrations資料夾就會產生SeedData
      PM> Add-Migration seeddata  
      PM> Update-Database  

route's People

Contributors

robbinhsu avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.