Nikhil Mittal
Nikhil Mittal

Reputation: 31

Web API 404 Error

I am getting 404 Error while calling web api method...

below is my code

Web API Controller:

[EnableCors(origins: "*", headers: "*", methods: "*")]
public class HomeController : ApiController
{
    private  HomeDataProvider homeDataProvider = new HomeDataProvider();

    // GET api/<controller>
    [AllowAnonymous]
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }

[AllowAnonymous]
    [HttpPost]
    //[Authorization]
    public List<User> Test_API(HomeSearch searchParam)
    {
        List<User> Test_User = null;
        try
        {
            Test_User = homeDataProvider.Test_DataProvider(searchParam);
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {

        }
        return Test_User;
    }

Web API Config

        // Web API configuration and services

        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "API Default",
            routeTemplate: "webapi/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}

Web API Web.config

      <appSettings></appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5.2" />
    <httpRuntime targetFramework="4.5.2" />
        <roleManager enabled="false" />
    <!--<roleManager defaultProvider="AspNetWindowsTokenRoleProvider" 
enabled="true" />-->
  </system.web>
  <system.webServer>
  <validation validateIntegratedModeConfiguration="false" />
  <modules runAllManagedModulesForAllRequests="true">
</modules>
  <handlers>
   </handlers>

      </system.webServer>

I am using windows 10, vs 2015, iis 10
url i am calling
http://localhost/webapi/home/get
Error: 404, static handler error

I searched and tried all options on stackoverflow and other sources but all in vain

Upvotes: 1

Views: 1557

Answers (4)

Waleed.alhasan
Waleed.alhasan

Reputation: 137

The problem appeared to me, and solved with unlikely solution: Just by Enabling Directory Browsing in IIS Website features view.

Upvotes: 0

Erik Nagel
Erik Nagel

Reputation: 376

Publishing a WebApi on IIS (Windows 10) and localhost

These are just my personal experiences, not a complete guide, but it worked for me.

Publishing in Visual Studio 2017/2019

  • Profile Publish method: File System
  • Target location points to the directory you want to publish to, i.e. "C:\Develop\MyWebApi\published"

Notice: If a WebApi works in Visual Studio Debugger, it doesn't mean, that it works in IE too. Therefore you'll have to make additional settings, see below.

IIS itself has to be configured to generally work with WebApis:

  • Control Panel/Programs/Programs and Features/Turn Windows features on or off/Internet Information Services/World Wide Web Services/Application Development Features/ select everything except CGI.

Configuration IIS Application Pool (Computer Management/Services and Applications/Internet Information Services (IIS) Manager/expand your computer/Application Pools/Add Application Pool...)

  • Name: freely selectable, i.e.. "WebApiLocalhost"
  • .Net CLR-Version: 4.0 (don't worry, your WebApi can have a higher level)
  • Identity: "LocalSystem"

    Normally recommended: "ApplicationPoolIdentity"(default). This user then must have file-system rights (read and execute) on the WebApi's publishing-directory ("C:\Develop\MyWebApi\published" in our example), and on all directories, the WebApi may address later. Possible problem with "ApplicationPoolIdentity" is that it's pseudo-user "IIS AppPool\<pool name>" exists only for the local computer and not for the domain. This can lead to problems when assigning permissions to folders of foreign servers.

    Notice: If you only need rights to local folders and want to use the pseudo-user "IIS AppPool\<pool name>" and your computer is in a domain, you cannot find this user by the default way. You have to select your local computer via the button "Locations..." first.

    For the identity "LocalSystem", the system user "IUSR" must receive the corresponding file system rights.

    A third option is to hardwire the identity to the local user with password. But this has the disadvantage that password changes always have to be trailed here, which is often forgotten.

Leave the other options at defaults.

Configuration WebSite (I tried HTTP only)

  • Bindings: HTTP, select a free Port i.e. 8094, no IP Address, no Host Name
  • Application Pool: your application pool, in our example: WebApiLocalhost
  • Physical Path: points to the WebApi's "Publish"-Directory, here "...MyWebApi/published"

Attention:

  • %LOCALAPPDATA% in IIS-Webapplications under ApplicationPoolIdentity "LocalSystem" points to "C:\Windows\system32\config\systemprofile\AppData\Local\" and not to Vc:\Users\\AppData\Local\". That means: If you have additional application-files like config.xmls or similar below %LOCALAPPDATA%, make sure that they exists in the IUSR's %LOCALAPPDATA% too.

Attention:

  • The system user "IUSR" must have read and execute rights to the Publish folder of the WebApi (in the example "C:\Develop\MyWebApi\published") as well as to all folders used within the WebApi application. So possibly on: "c:\Windows\system32\config\systemprofile\AppData\Local..." and possibly on external directories.

Attention:

  • After client-specific changes, especially in the Javascript environment, before a new test, necessarily delete the browser cache (Chrome via Ctrl-Shift-Del).

Attention:

  • "c:\ Users\\AppData\Local\Microsoft\WebsiteCache\" may contain outdated versions of application files - I searched for hours. Solution: Delete application-specific folders below ...\WebsiteCache\ regularly.

Upvotes: 0

Nikhil Mittal
Nikhil Mittal

Reputation: 31

I figured out answer

Actually: In Web API settings: i selected : Local iis, localhost/webapi... create virtual directory so my route was localhost/webapi/webapi/home/get Thanks Alex for help and probable solutions you posted...

Upvotes: 2

Alex
Alex

Reputation: 345

I think you either need to add {action} to your route template or try calling the endpoint without get in the URL like so:

http://localhost/webapi/home

Upvotes: 0

Related Questions