0

What I want is, API which takes email Id as a request object & check whether that email Id is present in Database or not.If email id is present Api will return true as a response else false.

What I have Done is:-

I have define one of the route for Emaiid as follow:-

 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}))+$" }
     );

I have also tried this :-

 config.Routes.MapHttpRoute(
         name: "ApiEmailId",
         routeTemplate: "api/{controller}/{action}/{email}",
         defaults: null,
        constraints: new { name = @"^[a-z]+$" }
     );

Also I have written actionresult as follow:-

 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);
        }

When i Hit the API using Postman:-
url:-

GET http://localhost/api/tblEmployeeRecords/GetEmailID/firstname.lastName@companyname.onmicrosoft.com

error, I am getting :-

<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>

Please kindly help me, Where I am wrong, what will be solution for this?

3 Answers3

1

Did you allow dot in web api url?

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

You can have more information about allowing dot in url at Dot character '.' in MVC Web API 2 for request such as api/people/STAFF.45287

Hien Nguyen
  • 24,551
  • 7
  • 52
  • 62
0

Please add your API port number after localhost

For example:

GET http://localhost:your port number(56586)/api/tblEmployeeRecords/GetEmailID/firstname.lastName@companyname.onmicrosoft.com
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
0

there are three reasons why you would get a 404.

  1. you issue the request with the wrong URL
  2. the email you pass as parameter does not exist and your controller method returns 404.
  3. your email address has some characters which break the route.

So, first check if you are hitting the controller at all. Put a breakpoint on the first line in the controller method, fire the call again and see if you hit it. If you do, keep debugging to see what's going on.

If you do not hit the controller method, then your route is wrong. To sort this out put a RoutePrefix on the controller and a Route attribute on your method. Once you do that, then hit the method again.

so your code could look like this:

[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);
        }

with such a setup in place your call becomes:

GET http://localhost/api/employeeRecords/GetEmailID/urlencoded email

adjust this URL, using the attributes to match whatever you want and make sure you make any required changes to your config.Routes object

UrlEncode the email address before you pass it in the URL.

If you need to read more about these things then have a look here: https://learn.microsoft.com/en-us/aspnet/web-api/overview/web-api-routing-and-actions/create-a-rest-api-with-attribute-routing

Andrei Dragotoniu
  • 6,155
  • 3
  • 18
  • 32