C#ASP.NET WebApi路由模板不使用uri参数
我有一个具有以下控制器和路由的ASP.NET WebAPI应用程序: WebApiConfig.csC#ASP.NET WebApi路由模板不使用uri参数,c#,asp.net,asp.net-web-api,asp.net-mvc-routing,C#,Asp.net,Asp.net Web Api,Asp.net Mvc Routing,我有一个具有以下控制器和路由的ASP.NET WebAPI应用程序: WebApiConfig.cs var constraintResolver = new DefaultInlineConstraintResolver(); constraintResolver.ConstraintMap.Add("validDate", typeof(DateConstraint)); 控制器 [HttpGet] [Route("deleted/{from:validDate?}/{to:validDa
var constraintResolver = new DefaultInlineConstraintResolver();
constraintResolver.ConstraintMap.Add("validDate", typeof(DateConstraint));
控制器
[HttpGet]
[Route("deleted/{from:validDate?}/{to:validDate?}", Name = "GetDeletedData")]
[SwaggerResponse(HttpStatusCode.OK, Type = typeof(IEnumerable<SimpleClass>), Description = "response")]
public async Task<HttpResponseMessage> Get(
[FromUri]string from = "",
[FromUri]string to = ""
)
{
}
如何确保只有在传递了正确的参数或抛出404 not found错误时才命中路由?您可以简单地设置所需的参数(问号表示它们是可选的)
[HttpGet]
[路由(“已删除/{from:validDate}/{to:validDate}”,Name=“getdeletedata”)]
[SwaggerResponse(HttpStatusCode.OK,Type=typeof(IEnumerable),Description=“response”)]
公共异步任务获取(
[FromUri]字符串from=“”,
[FromUri]字符串至=“”
)
{
}
您必须省略约束上的问号和参数的默认值:
[HttpGet]
[Route("deleted/{from:validDate}/{to:validDate}", Name = "GetDeletedData")]
[SwaggerResponse(HttpStatusCode.OK, Type = typeof(IEnumerable<SimpleClass>), Description = "response")]
public async Task<HttpResponseMessage> Get(
[FromUri]string from,
[FromUri]string to
)
{
}
[HttpGet]
[路由(“已删除/{from:validDate}/{to:validDate}”,Name=“getdeletedata”)]
[SwaggerResponse(HttpStatusCode.OK,Type=typeof(IEnumerable),Description=“response”)]
公共异步任务获取(
[FromUri]字符串来自,
[FromUri]字符串到
)
{
}
为什么要重新发明轮子
已经存在一个datetime
约束
另外,您可以做的只是将最后一个日期设置为可选,这样,如果提供了起始日期,它将使用起始日期到当前日期进行过滤
[HttpGet]
[Route("deleted/{from:datetime}/{to:datetime?}", Name = "GetDeletedData")]
[SwaggerResponse(HttpStatusCode.OK, Type = typeof(IEnumerable<SimpleClass>), Description = "response")]
public async Task<HttpResponseMessage> Get(DateTime from, DateTime? to = null)
{
//....
}
更新
如果两个参数都是可选模式
[HttpGet]
[Route("deleted/{from:validDate}/{to:validDate}", Name = "GetDeletedData")]
[SwaggerResponse(HttpStatusCode.OK, Type = typeof(IEnumerable<SimpleClass>), Description = "response")]
public async Task<HttpResponseMessage> Get(
[FromUri]string from = "",
[FromUri]string to = ""
)
{
}
//GET products/deleted - including query string will hit this
//GET products/deleted?adfear=2016-07-01 03:30:05&adfaewr=2016-07-01 03:30:05
//GET products/deleted/2016-01-01
//GET products/deleted/2016-01-01/2016-03-31
[HttpGet]
[Route("deleted/{from:datetime?}/{to:datetime?}", Name = "GetDeletedData")]
public async Task<HttpResponseMessage> Get(DateTime? from = null, DateTime? to = null) {
//...
}
你可以在行动中使用它
// GET products/deleted
// GET products/deleted?from=2016-01-01&to=2016-03-31
[HttpGet]
[Route("deleted", Name = "GetDeletedData")]
public async Task<HttpResponseMessage> Get([FromUri]DateRangeFilter filter) {
//...
}
//获取产品/删除
//获取产品/删除?从=2016-01-01和到=2016-03-31
[HttpGet]
[路由(“已删除”,Name=“GetDeletedData”)]
公共异步任务获取([FromUri]DateRangeFilter筛选器){
//...
}
还记得通过过滤器或在操作中进行模型验证。但我的要求是字段可以是可选的我的要求是字段可以是可选的如果它们都是可选的,那么对默认的
/deleted
的调用将与此操作冲突。这是因为Get()
和Get(DateTime?from=null,DateTime?to=null)
被认为是相同的,框架将不知道使用哪一个,如果字段是可选的,如何实现我的需求?有没有更好的方法来实现这一点?创建另一个操作来处理DeletedHats的默认路由。但同样的要求是,只需要创建一个操作:(.是否可以通过使用一些复杂类型并使用[FromUri]来实现?
//GET products/deleted
[HttpGet]
[Route("deleted")]
public async Task<HttpResponseMessage> Get() {
//...
}
//GET products/deleted/2016-01-01
//GET products/deleted/2016-01-01/2016-03-31
[HttpGet]
[Route("deleted/{from:datetime}/{to:datetime?}", Name = "GetDeletedData")]
public async Task<HttpResponseMessage> Get(DateTime from, DateTime? to = null) {
//...
}
//GET products/deleted
//GET products/deleted/{from}
//GET products/deleted/{from}/{to}
//GET products/deleted - including query string will hit this
//GET products/deleted?adfear=2016-07-01 03:30:05&adfaewr=2016-07-01 03:30:05
//GET products/deleted/2016-01-01
//GET products/deleted/2016-01-01/2016-03-31
[HttpGet]
[Route("deleted/{from:datetime?}/{to:datetime?}", Name = "GetDeletedData")]
public async Task<HttpResponseMessage> Get(DateTime? from = null, DateTime? to = null) {
//...
}
public class DateRangeFilter {
public DateTime? from { get; set; }
public DateTime? to { get; set; }
}
// GET products/deleted
// GET products/deleted?from=2016-01-01&to=2016-03-31
[HttpGet]
[Route("deleted", Name = "GetDeletedData")]
public async Task<HttpResponseMessage> Get([FromUri]DateRangeFilter filter) {
//...
}