我想要的是一个将电子邮件ID作为请求对象并检查数据库中是否存在该电子邮件ID的API。如果存在电子邮件ID,则Api将返回true作为响应,否则返回false。
我做的是:-
我为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]+$" }
);
我的书面行动结果如下:-
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);
}
当我使用Postman打API时:-
网址:-
GET http://localhost/api/tblEmployeeRecords/GetEmailID/firstname.lastName@companyname.onmicrosoft.com
错误,我得到:-
<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>
请帮助我,如果我错了,对此有什么解决方案?
答案 0 :(得分:0)
您允许在Web api网址中添加点吗?
<configuration>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
您可以在以下位置获得有关在URL中允许点的更多信息 Dot character '.' in MVC Web API 2 for request such as api/people/STAFF.45287
答案 1 :(得分:0)
请在localhost后添加您的API端口号
例如:
images.forEach(function (image) {
var img = `<img src="http://localhost/storage/${product.image}"`;
});
答案 2 :(得分:0)
您会得到404的三个原因。
因此,首先检查您是否完全按下了控制器。在控制器方法的第一行上放置一个断点,再次触发该调用,看看是否命中它。如果这样做,请继续调试以查看发生了什么。
如果您未按控制器方法,则您的路线是错误的。为了解决这个问题,在控制器上放置一个RoutePrefix,在方法上放置一个Route属性。完成后,请再次点击该方法。
因此您的代码应如下所示:
[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);
}
有了这样的设置,您的通话就会变成:
获取http://localhost/api/employeeRecords/GetEmailID/urlencoded电子邮件
调整此URL,使用属性匹配所需的任何内容,并确保对config.Routes对象进行必要的更改
在您通过URL传递电子邮件地址之前,先对其进行编码。
如果您需要阅读更多有关这些内容的信息,请在这里查看:https://docs.microsoft.com/en-us/aspnet/web-api/overview/web-api-routing-and-actions/create-a-rest-api-with-attribute-routing