2016-08-31 60 views
0

我用下面的代码在asp core属性约路由与空参数

[HttpGet] 
    [Route("all/{q:alpha}/{begin:int}/{pageSize:int}/{sortBy:alpha}/{sortOrder:alpha}")] 
    public IActionResult GetAll(string q, int begin, int pageSize, string sortBy, bool sortOrder) 
    { 
     return Json(_repository.GetItemsByPage(q, begin, pageSize, sortBy, sortOrder)); 
    } 

它应该是可能的,“Q”是空的。没有属性路由一切工作正常。下面的请求被工作:

http://localhost/api/all/?q=&begin=1&pagesize=3&sortBy=title&sortOrder=false 

有了路由请求中的属性是:

http://localhost/api/all//1/3/title/false 

如何使之成为一个空值(Q)工作?

回答

0

根据我的建议,可选参数必须结束。

[HttpGet] 

[Route("all/{begin:int}/{pageSize:int}/{sortBy:alpha}/{sortOrder:alpha}/{q:alpha}")] 
public IActionResult GetAll(string q, int begin, int pageSize, string sortBy, bool sortOrder) 
{ 
    return Json(_repository.GetItemsByPage(q, begin, pageSize, sortBy, sortOrder)); 
} 

通过这你的两个网址将工作。

http://localhost/api/all/?q=&begin=1&pagesize=3&sortBy=title&sortOrder=false 

http://localhost/api/all/1/3/title/false 

http://localhost/api/all/1/3/title/false/value of q 

现在,如果您的方法有多个可选参数,有多种方法可以解决问题。

http://localhost/api/all/1/3/title/false?q=1&optinal2=value 

或者创建第二个API方法。

+0

谢谢。我在最后移动了可选参数并使用:'{q:alpha?}' –