Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Asp Net核心控制器URL参数_C#_Asp.net Core_Asp.net Core Routing - Fatal编程技术网

C# Asp Net核心控制器URL参数

C# Asp Net核心控制器URL参数,c#,asp.net-core,asp.net-core-routing,C#,Asp.net Core,Asp.net Core Routing,我有控制器类如下: namespace OptionsAPI.Controllers { [Route("api/[controller]")] public class OptionsController : Controller { HttpGet("{symbol}/{date}")] public IActionResult Chain(string symbol, string date) {

我有
控制器
如下:

namespace OptionsAPI.Controllers
{
    [Route("api/[controller]")]
    public class OptionsController : Controller
    {    
        HttpGet("{symbol}/{date}")]
        public IActionResult Chain(string symbol, string date)
        {
            DateTime quotedate = System.DateTime.Parse(date);
        }
    }
}
http://127.0.0.1:5000/api/options/Chain/symbol=SPX&date=2019-01-03T10:00:00
当我尝试通过URL调用chain函数时,如下所示:

namespace OptionsAPI.Controllers
{
    [Route("api/[controller]")]
    public class OptionsController : Controller
    {    
        HttpGet("{symbol}/{date}")]
        public IActionResult Chain(string symbol, string date)
        {
            DateTime quotedate = System.DateTime.Parse(date);
        }
    }
}
http://127.0.0.1:5000/api/options/Chain/symbol=SPX&date=2019-01-03T10:00:00
我得到这个错误:

格式异常:字符串“symbol=SPX&date=2019-01-03T10:00:00”未被识别为有效的日期时间。有一个以索引“0”开头的未知单词


似乎“SPX”和“date”被连接为一个
string
。调用此
URL
的正确方法是什么?

操作上的给定路由模板

[HttpGet("{symbol}/{date}")]
以及控制器上的模板

[Route("api/[controller]")]
期望

http://127.0.0.1:5000/api/options/SPX/2019-01-03T10:00:00
但是这个名字叫做URI

http://127.0.0.1:5000/api/options/Chain/symbol=SPX&date=2019-01-03T10:00:00
将URL中的
映射到
符号
,其余映射到
日期
,解析时将失败

要获得所需的URI,模板需要如下所示

[Route("api/[controller]")]
public class OptionsController : Controller {

    //GET api/options/chain?symbol=SPX&date=2019-01-03T10:00:00
    [HttpGet("Chain")]
    public IActionResult Chain(string symbol, string date) {
        //...
    }
}
参考文献


参考动作上的给定路线模板

[HttpGet("{symbol}/{date}")]
以及控制器上的模板

[Route("api/[controller]")]
期望

http://127.0.0.1:5000/api/options/SPX/2019-01-03T10:00:00
但是这个名字叫做URI

http://127.0.0.1:5000/api/options/Chain/symbol=SPX&date=2019-01-03T10:00:00
将URL中的
映射到
符号
,其余映射到
日期
,解析时将失败

要获得所需的URI,模板需要如下所示

[Route("api/[controller]")]
public class OptionsController : Controller {

    //GET api/options/chain?symbol=SPX&date=2019-01-03T10:00:00
    [HttpGet("Chain")]
    public IActionResult Chain(string symbol, string date) {
        //...
    }
}
参考文献

参考文献