Asp.net mvc ASP.NET MVC API-通过电子邮件Id获取记录

Asp.net mvc ASP.NET MVC API-通过电子邮件Id获取记录,asp.net-mvc,entity-framework,asp.net-web-api,asp.net-web-api-routing,Asp.net Mvc,Entity Framework,Asp.net Web Api,Asp.net Web Api Routing,我想要的是,API将电子邮件Id作为请求对象&检查数据库中是否存在该电子邮件Id。如果存在电子邮件Id,API将返回true作为响应,否则返回false 我所做的是:- 我已将Emaiid的一条路线定义如下:- config.Routes.MapHttpRoute( name: "ApiEmailId", routeTemplate: "api/{controller}/{action}/{email}", defaults: null,

我想要的是,API将电子邮件Id作为请求对象&检查数据库中是否存在该电子邮件Id。如果存在电子邮件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:-
网址:-

错误,我得到:-

<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>
HTTP错误404.0-找不到
您正在查找的资源已被删除、名称已更改或暂时不可用。
最可能的原因:
  • Web服务器上不存在指定的目录或文件
  • URL包含一个印刷错误
  • 自定义筛选器或模块(如URLScan)限制对文件的访问

请帮助我,如果我错了,该怎么办?

您是否允许在web api url中使用点

<configuration>
    <system.webServer>
        <modules runAllManagedModulesForAllRequests="true" />

您可以在以下网址获得有关允许在url中使用点的更多信息:

请在localhost之后添加API端口号

例如:

GET http://localhost:your port number(56586)/api/tblEmployeeRecords/GetEmailID/firstname.lastName@companyname.onmicrosoft.com

有三个原因可以让你得到404

  • 您使用错误的URL发出请求
  • 作为参数传递的电子邮件不存在,控制器方法返回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);
            }
    
    有了这样的设置,您的通话将变成:

    收到电子邮件

    调整此URL,使用属性匹配您想要的任何内容,并确保对config.Routes对象进行任何必要的更改

    URL在将电子邮件地址传递到URL之前对其进行编码


    如果您需要阅读更多有关这些内容的信息,请查看此处:

    控制器名称是什么?您的控制器是否真的命名为
    tblEmployeeRecords
    ?@ChetanRanpariya tblEmployeeRecords is controllername@BrendanGreenYesno我不允许在web api url中使用点。。。将进行这些修改并让您知道
    [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);
            }