Angularjs 我们可以在WebApicController中编写多个get/post自定义方法吗??如果是,如何将字符串参数传递给该自定义方法?
我是WebApi和MVC新手…请帮助我继续。 下面是我面临问题的场景 在WebApi控制器中,我想编写自定义/用户定义的get/post方法 我想把字符串参数作为参数传递。 但这是一个错误。。。。 默认情况下,它只接受整数参数 下面是我需要添加到WebApicController的方法Angularjs 我们可以在WebApicController中编写多个get/post自定义方法吗??如果是,如何将字符串参数传递给该自定义方法?,angularjs,model-view-controller,asp.net-web-api,routing,Angularjs,Model View Controller,Asp.net Web Api,Routing,我是WebApi和MVC新手…请帮助我继续。 下面是我面临问题的场景 在WebApi控制器中,我想编写自定义/用户定义的get/post方法 我想把字符串参数作为参数传递。 但这是一个错误。。。。 默认情况下,它只接受整数参数 下面是我需要添加到WebApicController的方法 [HttpGet] public bool IsExists(string email) { //code: It will return true if email already exists else re
[HttpGet]
public bool IsExists(string email)
{
//code: It will return true if email already exists else returns false.
}
甚至我在默认API路由之前在webApi.config文件中添加了自定义路由
routes.MapHttpRoute(
name: "CustomApi",
routeTemplate: "api/{Controller}/{Action}/{email:string}",
defaults: null
);
提前感谢。:) 如果您使用的是WebApi 2,则可以使用:
[Route("api/emails/exists/{email}")]
[HttpGet]
public bool IsExists(string email)
{
//code: It will return true if email already exists else returns false.
}
这将获取在{email}
中传递的值,并将其传递到方法中。尽管此引用默认情况下,“:string”不是有效的约束
尝试从路线定义中删除:字符串。谢谢您的回复。下面是我在WebApicController中更改的代码。[System.Web.Http.Route(“api/ApplicationAPI/isExists/{email}”)][System.Web.Http.HttpGet]私有bool isExists(字符串电子邮件){//code}和in Service.js,用于调用上述操作方法。this.isExists=function(email){return$http.get(“/api/ApplicationAPI/isExists/”+email);}但是我得到了一个类似这样的错误:错误:5:与“~/Controllers.ApplicationAPIController.isExists(string)”匹配的最佳重载方法有一些无效参数错误:6:参数1:无法从“int”转换为“string”,感谢您的反馈。作为检查,您可以将
{return$http.get(“/api/ApplicationAPI/isExists/”+email);}
更改为测试的硬编码电子邮件地址吗?看起来该方法正在被调用,但出于某种原因,它使用int。可能类似于{return$http.get(“/api/ApplicationAPI/isExists/myemail@mydomain.xyz);}