我要的是,API这需要电子邮件ID作为请求对象和检查电子邮件ID是否存在于数据库或not.If电子邮件ID存在API将作为其他错误响应返回true。
我做了什么是: -
我定义Emaiid路线如下之一: -
config.Routes.MapHttpRoute(
name: "ApiEmailId",
routeTemplate: "api/{controller}/{action}/{email}",
defaults: null,
constraints: new { name = @"^([\w\-\.]+)@((\[([0-9]{1,3}\.){3}[0-9]{1,3}\])|(([\w\-]+\.)+)([a-zA-Z]{2,4}))+$" }
);
我也试过这样: -
config.Routes.MapHttpRoute(
name: "ApiEmailId",
routeTemplate: "api/{controller}/{action}/{email}",
defaults: null,
constraints: new { name = @"^[a-z]+$" }
);
此外,我已经写的ActionResult如下: -
public IHttpActionResult GetEmailID(string email)
{
//New action condition
//To get employee records
var tblCountryName = from a in db.tblEmployeeRecords
where a.userName == email
select a;
if (tblCountryName == null)
{
return NotFound();
}
return Ok(tblCountryName);
}
我击球的时候使用邮差的API: - 网址: -
GET http://localhost/api/tblEmployeeRecords/GetEmailID/[email protected]
错误,我越来越: -
<h3>HTTP Error 404.0 - Not Found</h3>
<h4>The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.</h4>
</div>
<div class="content-container">
<fieldset>
<h4>Most likely causes:</h4>
<ul>
<li>The directory or file specified does not exist on the Web server.</li>
<li>The URL contains a typographical error.</li>
<li>A custom filter or module, such as URLScan, restricts access to the file.</li>
</ul>
</fieldset>
请好心帮我,如果我错了,这将是该解决方案?
你允许网络API URL点?
<configuration>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
你可以有关于允许圆点网址在Dot character '.' in MVC Web API 2 for request such as api/people/STAFF.45287更多信息
请后本地主机添加API端口号
例如:
GET http://localhost:your port number(56586)/api/tblEmployeeRecords/GetEmailID/[email protected]
有三个原因,你会得到一个404。
因此,首先检查您是否在所有打控制器。放在控制器方法中的第一行设置一个断点,再次触发通话,看看你打它。如果你这样做,不断的调试,看看发生了什么事情。
如果你不打控制方法,那么你的路线是错误的。要排序了这一点把RoutePrefix控制器和你的方法路由属性上。一旦你这样做,然后再次命中方法。
所以你的代码看起来是这样的:
[RoutePrefix("api/employeeRecords")]
public class yourControllerName: ApiController
//more controller code
[Route("GetEmailId/{email})"]
public IHttpActionResult GetEmailID(string email)
{
//start doing model validation. no point doing anything if the email passed in is empty ....
if ( string.IsNullOrWhitespace(email) )
{
//return something appropriate for your project
}
//New action condition
//To get employee records
var tblCountryName = from a in db.tblEmployeeRecords
where a.userName == email
select a;
if (tblCountryName == null)
{
return NotFound();
}
return Ok(tblCountryName);
}
有了这样的设置你的电话就变成了:
GET http://localhost/api/employeeRecords/GetEmailID/urlencoded电子邮件
调整这个URL,利用属性相匹配任何你想要的,并确保你做任何需要修改您的config.Routes对象
您在URL中传递之前进行urlencode的电子邮件地址。
如果您需要了解更多关于这些事情,然后看看这里:https://docs.microsoft.com/en-us/aspnet/web-api/overview/web-api-routing-and-actions/create-a-rest-api-with-attribute-routing