我正在使用mvc 6创建一个web api.现在我正在尝试从我的数据库中获取一个元素.此表中的键是一个字符串(电子邮件地址).我无法访问此数据库,因此我无法更改此表的密钥.
现在,当创建一个演示webapi时,我能够创建一个控制器来根据一个int键提取项目.但是当尝试通过字符串获取元素时,程序崩溃了.
[Route("api/[controller]")]
public class TodoController : Controller
{
[HttpGet("{id:string}", Name = "GetByIdRoute")]
public IActionResult GetById (string id)
{
var item = _items.FirstOrDefault(x => x.Id == id);
if (item == null)
{
return HttpNotFound();
}
return new ObjectResult(item);
}
}
Run Code Online (Sandbox Code Playgroud)
当试图访问此路径(example.com/api/Todo/key)时键是字符串我在startup.cs中得到异常
浏览器中的异常如下:
System.InvalidOperationException路径'api/Todo/{id:string}'上的约束条目'id' - 'string'无法通过类型'DefaultInlineConstraintResolver'的约束解析器解析.
代码中断的startup.cs的一部分是:
// Add MVC to the request pipeline.
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller}/{action}/{id?}",
defaults: new { controller = "Home", action = "Index" });
});
Run Code Online (Sandbox Code Playgroud)
我似乎无法弄清楚为什么我不允许通过一个字符串键获得一个项目.这是否可能,如果是这样,我做错了什么?
这是我目前的if语句:
if (excel_getValue("A" + i) == "" &&
excel_getValue("A" + (i + 1)) == "" &&
excel_getValue("A" + (i + 2)) == "" &&
excel_getValue("A" + (i + 3)) == "" &&
excel_getValue("A" + (i + 4)) == "" &&
excel_getValue("A" + (i + 5)) == "" &&
excel_getValue("A" + (i + 6)) == "" &&
excel_getValue("A" + (i + 7)) == "" &&
excel_getValue("A" + (i + 8)) == "")
Run Code Online (Sandbox Code Playgroud)
无论如何,我可以减少这种说法吗?我应该在for循环中构建这个if语句吗?
顺便说一下,这个if语句如果已经在forloop中并且它使用了forloop的i