透過 RouteAttribute 可以在Request打進來的第一步就做好參數的檢查,筆記一下幾種用法
型別檢查
[Route("{id:int}")]
[HttpGet]
public int Value(string id)
{
return id;
}
長度檢查
[Route("{id:max(10)}")]
[HttpGet]
public int Value2(int id)
{
return id;
}
正則表達式
[Route("{id:regex(^\\d{{3}}-\\d{{2}}-\\d{{4}}$)}")]
[HttpGet]
public string Value3(string id)
{
return id;
}
必填檢查
[Route("{name:required}")]
[HttpGet]
public string Value4(string name)
{
return name;
}
貪婪模式
路由後的所由內容都是這個參數的值
[Route("{*name}")]
[HttpGet]
public string Value5(string name)
{
return name;
}
自定義約束
自定了一個同 {id:int} 的型別檢查,需要在 Startup 加入
[Route("{name:MyRouteConstraint}")]
[HttpGet]
public string Value6(string name)
{
return name;
}
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddRouting(options =>
{
options.ConstraintMap.Add("MyRouteConstraint", typeof(MyConstraint));
});
}
public class MyConstraint : IRouteConstraint
{
public bool Match(HttpContext httpContext, IRouter route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection)
{
if (routeDirection == RouteDirection.IncomingRequest)
{
var value = values[routeKey];
if (int.TryParse(value.ToString(), out _))
{
return true;
}
}
return false;
}
}
產生 Url
若需要依照路由產生對應的 Url,在.NetCore提供了一個類別 LinkGenerator
[HttpGet]
public string Value7([FromServices] LinkGenerator generator)
{
var pathByAction = generator.GetPathByAction(HttpContext, "Value2", "WeatherForecast", new
{
id = 5
});
return pathByAction;
}
NonAction
若定義的方法不屬於給對外呼叫的 Api,可以加上 [NonActionAttribute]
[NonAction]
public string Value8([FromServices] LinkGenerator generator)
{
return "OK";
}
Microsoft Docs
Sample Code
透過 RouteAttribute 可以在Request打進來的第一步就做好參數的檢查,筆記一下幾種用法
型別檢查
[Route("{id:int}")] [HttpGet] public int Value(string id) { return id; }
長度檢查
[Route("{id:max(10)}")] [HttpGet] public int Value2(int id) { return id; }
正則表達式
[Route("{id:regex(^\\d{{3}}-\\d{{2}}-\\d{{4}}$)}")] [HttpGet] public string Value3(string id) { return id; }
必填檢查
[Route("{name:required}")] [HttpGet] public string Value4(string name) { return name; }
貪婪模式
路由後的所由內容都是這個參數的值
[Route("{*name}")] [HttpGet] public string Value5(string name) { return name; }
自定義約束
自定了一個同 {id:int} 的型別檢查,需要在 Startup 加入
[Route("{name:MyRouteConstraint}")] [HttpGet] public string Value6(string name) { return name; } //Startup public void ConfigureServices(IServiceCollection services) { services.AddControllers(); services.AddRouting(options => { options.ConstraintMap.Add("MyRouteConstraint", typeof(MyConstraint)); }); } public class MyConstraint : IRouteConstraint { public bool Match(HttpContext httpContext, IRouter route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection) { if (routeDirection == RouteDirection.IncomingRequest) { var value = values[routeKey]; if (int.TryParse(value.ToString(), out _)) { return true; } } return false; } }
產生 Url
若需要依照路由產生對應的 Url,在.NetCore提供了一個類別 LinkGenerator
[HttpGet] public string Value7([FromServices] LinkGenerator generator) { var pathByAction = generator.GetPathByAction(HttpContext, "Value2", "WeatherForecast", new { id = 5 }); return pathByAction; }
NonAction
若定義的方法不屬於給對外呼叫的 Api,可以加上 [NonActionAttribute]
[NonAction] public string Value8([FromServices] LinkGenerator generator) { return "OK"; }
Microsoft Docs
Sample Code