【问题标题】:ASP.NET MVC API- Get record by email IdASP.NET MVC API-通过电子邮件 ID 获取记录
【发布时间】:2019-02-08 00:24:53
【问题描述】:

我想要的是,将电子邮件 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]+$" }
     );

我还写了如下的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);
        }

当我使用 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>

请帮帮我,我哪里错了,有什么解决办法?

【问题讨论】:

  • 控制器名称是什么?
  • 你的控制器真的叫tblEmployeeRecords吗?
  • @ChetanRanpariya tblEmployeeRecords 是控制器名称
  • @BrendanGreenYes

标签: asp.net-mvc entity-framework asp.net-web-api asp.net-web-api-routing


【解决方案1】:

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

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

您可以在以下位置获得有关在 url 中允许点的更多信息 Dot character '.' in MVC Web API 2 for request such as api/people/STAFF.45287

【讨论】:

  • 不,我不允许在 web api url 中使用点...将进行这些修改并让您知道
【解决方案2】:

请在 localhost 后添加您的 API 端口号

例如:

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

【讨论】:

    【解决方案3】:

    您会收到 404 的三个原因。

    1. 您使用错误的 URL 发出请求
    2. 您作为参数传递的电子邮件不存在,您的控制器方法返回 404。
    3. 您的电子邮件地址中有一些字符会破坏路由。

    所以,首先检查您是否完全按下了控制器。在控制器方法的第一行放置一个断点,再次触发调用,看看你是否击中它。如果你这样做了,请继续调试以查看发生了什么。

    如果你没有点击控制器方法,那么你的路线是错误的。要解决这个问题,请在控制器上放置一个 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 中传递电子邮件地址之前,对电子邮件地址进行 URL 编码。

    如果您需要了解有关这些内容的更多信息,请查看此处:https://docs.microsoft.com/en-us/aspnet/web-api/overview/web-api-routing-and-actions/create-a-rest-api-with-attribute-routing

    【讨论】:

      猜你喜欢
      • 2015-03-01
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-07
      • 1970-01-01
      • 2019-05-29
      相关资源
      最近更新 更多