Reputation: 31
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
Reputation: 137
The problem appeared to me, and solved with unlikely solution: Just by Enabling Directory Browsing in IIS Website features view.
Upvotes: 0
Reputation: 376
These are just my personal experiences, not a complete guide, but it worked for me.
Publishing in Visual Studio 2017/2019
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:
Configuration IIS Application Pool (Computer Management/Services and Applications/Internet Information Services (IIS) Manager/expand your computer/Application Pools/Add Application Pool...)
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)
Attention:
Attention:
Attention:
Attention:
Upvotes: 0
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
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