locked
Restful Web API not working (HTTP 404 Not Found) RRS feed

  • Question

  • User-1798917180 posted

    Hi,

    New to WebAPI Restful service. This is what I have done.

    Tried different approaches like attribute routing etc to pass multiple parameters

    1) Global.asax.cs (Application Start Event)

          void Application_Start(object sender, EventArgs e)

          {

                     RouteTable.Routes.MapHttpRoute(

                       name: "DefaultApi",

                      routeTemplate: "api/{controller}/{action}/{id}",

                     defaults: new { id = RouteParameter.Optional }

                    );

             }

    2)   Controller

            

    public class AccountController : ApiController

    {

    public IEnumerable<AccountModel> Get(int pClientID, int pProgramID)

    {

    NameValueCollection nvc = HttpUtility.ParseQueryString(Request.RequestUri.Query);

    int clientID = Convert.ToInt32(nvc["ClientID"]);

    int programID = Convert.ToInt32(nvc["ProgramID"]);

    return new AccountData().GetAccounts(pClientID, pProgramID);

    }

    }

    call web api:

    http://localhost:52724/api/Account?ClientID=19&ProgramID=93

    Error:

    HTTP Error 404.0 - Not Found

    The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.

    <fieldset style="width: 1032px; height: 97px;">

    Most likely causes:

    • The directory or file specified does not exist on the Web server.
    • The URL contains a typographical error.
    • A custom filter or module, such as URLScan, restricts access to the file.
    </fieldset>

    Detailed Error Information:

    Module    IIS Web Core
    Notification    MapRequestHandler
    Handler    StaticFile
    Error Code    0x80070002
    Requested URL    http://localhost:52724/api/Account?ClientID=19&ProgramID=93
    Physical Path    C:\Apps\WebApplication1\WebApplication1\api\Account
    Logon Method    Anonymous
    Logon User    Anonymous
    Request Tracing Directory    C:\Users\xxxxx\Documents\IISExpress\TraceLogFiles\WEBAPPLICATION1(2)

    Monday, May 14, 2018 4:23 PM

All replies

  • User-1798917180 posted

    Project properties - web - Project Url is set to http://localhost:52724/ and Start Action set to 'don't open a page. Wait for a request.....'

    Monday, May 14, 2018 4:26 PM
  • User475983607 posted

    The querystring parameters do not match the action method input parameters.

    public IEnumerable<AccountModel> Get(int pClientID, int pProgramID)
    http://localhost:52724/api/Account?ClientID=19&ProgramID=93

    Monday, May 14, 2018 4:30 PM
  • User475983607 posted

    Project properties - web - Project Url is set to http://localhost:52724/ and Start Action set to 'don't open a page. Wait for a request.....'

    Not sure what you're after here...

    Anyway a simple working test.

    // GET api/values?pClientID=1&pProgramID=2
    public int[] Get(int pClientID, int pProgramID)
    {
        return new int[] { pClientID, pProgramID };
    }

    URL 

    http://localhost:52803/api/values?pClientID=1&pProgramID=2

    Monday, May 14, 2018 4:36 PM
  • User-1798917180 posted

    Thank you mgeghard. Still getting the same error. I updated it to ClientID, ProgramID

    Monday, May 14, 2018 4:38 PM
  • Monday, May 14, 2018 4:41 PM
  • User-1798917180 posted

    I need to get a data from database by passing parameters to the stored procedure. Later, I should bind it to Telerik Rad Grid. I am trying to get data successfully from web api before binding.

    Monday, May 14, 2018 4:43 PM
  • User475983607 posted

    I need to get a data from database by passing parameters to the stored procedure. Later, I should bind it to Telerik Rad Grid. I am trying to get data successfully from web api before binding.

    Before you can access data you need to get the action working.  Is the 404 error fixed?

    Monday, May 14, 2018 4:50 PM
  • User1120430333 posted

    Thank you mgeghard. Still getting the same error. I updated it to ClientID, ProgramID

    You need to use something like Fiddler. free to use, and look at the raw URL at the time of the 404 error. If the URL and the parms on the URL to not match the method to be accessed then it is 404 'Not found'.  There is something wrong with the URL as to why the method cannot be found, like bad parm.

    http://www.asjava.com/tools/fiddler-tutorial-how-to-use-fiddler/

    Monday, May 14, 2018 4:54 PM
  • User-1798917180 posted

    DA924, I have Fiddler turned on.

    Below is what I see under Headers bottom Raw table

    HTTP/1.1 404 Not Found

    Cache-Control: private

    Content-Type: text/html; charset=utf-8

    Server: Microsoft-IIS/10.0

    X-SourceFiles: =?UTF-8?B?QzpcVGVzdEFwcHNcV2ViQXBwbGljYXRpb24xXFdlYkFwcGxpY2F0aW9uMVxhcGlcQWNjb3VudA==?=

    X-Powered-By: ASP.NET

    Date: Mon, 14 May 2018 18:34:01 GMT

    Content-Length: 4965

     

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>

    <title>IIS 10.0 Detailed Error - 404.0 - Not Found</title>

    <style type="text/css">

    <!--

    body{margin:0;font-size:.7em;font-family:Verdana,Arial,Helvetica,sans-serif;}

    code{margin:0;color:#006600;font-size:1.1em;font-weight:bold;}

    .config_source code{font-size:.8em;color:#000000;}

    pre{margin:0;font-size:1.4em;word-wrap:break-word;}

    ul,ol{margin:10px 0 10px 5px;}

    ul.first,ol.first{margin-top:5px;}

    fieldset{padding:0 15px 10px 15px;word-break:break-all;}

    .summary-container fieldset{padding-bottom:5px;margin-top:4px;}

    legend.no-expand-all{padding:2px 15px 4px 10px;margin:0 0 0 -12px;}

    legend{color:#333333;;margin:4px 0 8px -12px;_margin-top:0px;

    font-weight:bold;font-size:1em;}

    a:link,a:visited{color:#007EFF;font-weight:bold;}

    a:hover{text-decoration:none;}

    h1{font-size:2.4em;margin:0;color:#FFF;}

    h2{font-size:1.7em;margin:0;color:#CC0000;}

    h3{font-size:1.4em;margin:10px 0 0 0;color:#CC0000;}

    h4{font-size:1.2em;margin:10px 0 5px 0;

    }#header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-family:"trebuchet MS",Verdana,sans-serif;

    color:#FFF;background-color:#5C87B2;

    }#content{margin:0 0 0 2%;;}

    .summary-container,.content-container{background:#FFF;width:96%;margin-top:8px;padding:10px;;}

    .content-container p{margin:0 0 10px 0;

    }#details-left{width:35%;float:left;margin-right:2%;

    }#details-right{width:63%;float:left;overflow:hidden;

    }#server_version{width:96%;_height:1px;min-height:1px;margin:0 0 5px 0;padding:11px 2% 8px 2%;color:#FFFFFF;

    background-color:#5A7FA5;border-bottom:1px solid #C1CFDD;border-top:1px solid #4A6C8E;font-weight:normal;

    font-size:1em;color:#FFF;text-align:right;

    }#server_version p{margin:5px 0;}

    table{margin:4px 0 4px 0;width:100%;border:none;}

    td,th{vertical-align:top;padding:3px 0;text-align:left;font-weight:normal;border:none;}

    th{width:30%;text-align:right;padding-right:2%;font-weight:bold;}

    thead th{background-color:#ebebeb;width:25%;

    }#details-right th{width:20%;}

    table tr.alt td,table tr.alt th{}

    .highlight-code{color:#CC0000;font-weight:bold;font-style:italic;}

    .clear{clear:both;}

    .preferred{padding:0 5px 2px 5px;font-weight:normal;background:#006633;color:#FFF;font-size:.8em;}

    -->

    </style>

    </head>

    <body>

    <div id="content">

    <div class="content-container">

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

    </div>

    <div class="content-container">

    <fieldset><h4>Things you can try:</h4>

    <ul> <li>Create the content on the Web server.</li> <li>Review the browser URL.</li> <li>Check the failed request tracing log and see which module is calling SetStatus. For more information, click <a href="http://go.microsoft.com/fwlink/?LinkID=66439">here</a>. </li> </ul>

    </fieldset>

    </div>

    <div class="content-container">

    <fieldset><h4>Detailed Error Information:</h4>

    <div id="details-left">

    <table border="0" cellpadding="0" cellspacing="0">

    <tr class="alt"><th>Module</th><td>&nbsp;&nbsp;&nbsp;IIS Web Core</td></tr>

    <tr><th>Notification</th><td>&nbsp;&nbsp;&nbsp;MapRequestHandler</td></tr>

    <tr class="alt"><th>Handler</th><td>&nbsp;&nbsp;&nbsp;StaticFile</td></tr>

    <tr><th>Error Code</th><td>&nbsp;&nbsp;&nbsp;0x80070002</td></tr>

    </table>

    </div>

    <div id="details-right">

    <table border="0" cellpadding="0" cellspacing="0">

    <tr class="alt"><th>Requested URL</th><td>&nbsp;&nbsp;&nbsp;http://localhost:52724/api/Account?pClientID=1&amp;pProgramID=2</td></tr>

    <tr><th>Physical Path</th><td>&nbsp;&nbsp;&nbsp;C:\TestApps\WebApplication1\WebApplication1\api\Account</td></tr>

    <tr class="alt"><th>Logon Method</th><td>&nbsp;&nbsp;&nbsp;Anonymous</td></tr>

    <tr><th>Logon User</th><td>&nbsp;&nbsp;&nbsp;Anonymous</td></tr>

    <tr class="alt"><th>Request Tracing Directory</th><td>&nbsp;&nbsp;&nbsp;C:\Users\pkat644\Documents\IISExpress\TraceLogFiles\WEBAPPLICATION1(2)</td></tr>

    </table>

    <div class="clear"></div>

    </div>

    </fieldset>

    </div>

    <div class="content-container">

    <fieldset><h4>More Information:</h4>

    This error means that the file or directory does not exist on the server. Create the file or directory and try the request again.

    <p><a href="https://go.microsoft.com/fwlink/?LinkID=62293&amp;IIS70Error=404,0,0x80070002,15063">View more information &raquo;</a></p>

    </fieldset>

    </div>

    </div>

    </body>

    </html>

     

    Monday, May 14, 2018 6:40 PM
  • User475983607 posted

    Frankly, you're doing an HTTP GET.  The URL is pretty clear and no need for Fiddler.  I would use PostMan for testing.  Fidler is a proxy and has the ability to send HTTP requests but I prefer PostMan.

    I provided a working example.  Did you try the working example?

    Is there anyway you can post the source code and include the RouteConfig.cs file?

    Monday, May 14, 2018 6:46 PM
  • User1120430333 posted

    GET http://nym1-ib.adnxs.com/vevent?e=wqT_3QKHDPBCBwYAAAMA1gAFAQjm4OfXBRCnnMvn0Jm5lgEYuaqNhfHkwJ9AKjYJKVyPwvUoBEARKVyPwvUoBEAZAAAAANejFEAhKQ0SACkRJMgxAAAAwB6F4z8wz67IATiYAkCHB0g0UJuBqSFY5LUDYABou9g4eNvhBIABAYoBA1VTRJIFBvQYBJgBrAKgAfoBqAEBsAEAuAEBwAEFyAEC0AEA2AEA4AEA8AEA-gEJTVNOVVNFTjExkgKLA001UGR2alF3MG80WHBEMnBvR1M0QTQxMXY1TXFOMWZOeUJpZVh4TlQ0NldzMmdLNklIYWNLMWRfMnZoRE1NSWQweFdwWE51QXJ6U3Y2bHZJb2I5LUpiV1FmRkhUWkdHWjJWQVR0TlljaHp0Nmp4U0tQckxv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FMzFrTGdZOFo4a3RHOWpKNkJ5cExkODRuWE9WdzNXVjFkTXdQLWJqWC1MUzFyeUM5cEoxSG44bG0ySk8zU3hkTDBtR0t3NkF3M0lMX0FpUTRVbHhrZnZRd05xVPICLQoMREVMKd7wq1VSTBIdY2FzLnVzLmNyaXRlby5jb20vZGVsaXZlcnkvci-AAwCIAwGQAwCYAxegAwGqAxgaETg0Njk0MDYyMzQ5ODY0NDg3KgM5MDPAA6wCyAMA2AOmBeADAOgDAPgDAoAEAJIEBi91dC92M5gEAKIEDDY1LjMxLjYzLjIyOKgEs9EPsgQQCAUQBBisAiD6ASgCMAA4ArgEAMAEpfL8IMgEANoEAggB4AQB8ASB7FSIBQGYBQCgBeqCp_608NrpXMAFAMkFoTccAADwP9IFCQkJDGwAANgFAeAFAeoFFQoJcGFnZV90eXBlEghob21lAQ_wZOoFCgoGb3B0b3V0EgDqBScKA3JpZBIgYmQyYjc5NTZkN2QyNDRkOGEzYWQ2YzgyZmE5ZjhkNjTqBRYKC3Byb3ZpZGVyX2lkEgc3aGQ2NmZj8AXglAT6BQQIABAAkAYAmAYAuAYA&s=86dc601fd65aa36241d72f33cb94cdd2658764fd&referrer=http%3A%2F%2Fwww.msn.com%2F%3Fcobrand%3Dhp15-comm.msn.com%26ocid%3DHPCOMMDHP15%26pc%3DHRTE&type=nv&nvt=10&px=1093&py=471&bw=300&bh=250&sid=8585436191093147793&sv=127&tv=view7-1h&ua=ie11&pl=win&x=v&ct=0&tag_id=3282767&rr=20&pd=0&d=4.28&id=0&ic=0&d0=0&d25=0&d50=0&d75=0&d100=0&dm=90&ft=2 HTTP/1.1
    Accept: */*
    Referer: http://www.msn.com/?cobrand=hp15-comm.msn.com&ocid=HPCOMMDHP15&pc=HRTE
    Accept-Language: en-US,en;q=0.5
    Origin: http://www.msn.com
    Accept-Encoding: gzip, deflate
    User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; Touch; rv:11.0) like Gecko
    Host: nym1-ib.adnxs.com
    Connection: Keep-Alive

    As in the above example from Fiddler under the Raw tab on the line that had the 404 in the right-side screen pane, you should  see the URL that was sent including its parms. If the URL and its parms don't match what the method you are trying to access on the WebAPI, then it's 404 'not found'.

    You have to look and not stab in the dark and find out  what the URL looked like when it was sent by the program trying to access the WebAPI's method, at the time of the 404. 

    The above is just an example of what you should be looking for and it makes no difference if a Get or Post is being done. Fiddler will show you the raw URL that was sent.

    Monday, May 14, 2018 8:31 PM
  • User753101303 posted

    Hi,

    And with /api/Account?ClientID=19&ProgramID=93 and api/{controller}/{action}/{id} what is your {action} ?

    Have you tried first to start with something basic to check it works fine? Then change the route and do the corresponding changes you need (action and/or parameters) to immediately check it still works.

    For now do you want to use the http verbs to select the action or do you want to use explicit action names ?

    Monday, May 14, 2018 9:01 PM