简体   繁体   English

使用RouteAttribute和IIS应用程序会出现404错误

[英]Using RouteAttribute and IIS Application gives 404 error

I have created a webAPI project and I would like to start hosting it in IIS. 我已经创建了一个webAPI项目,并且想开始在IIS中托管它。

The method in the controller looks like this: 控制器中的方法如下所示:

    [TokenAuthentication]
    [HttpGet]
    [Authorize(Roles=Roles.PING)]
    [Route("~/Ping")]
    public DateTime Ping()
    {
        return DateTime.UtcNow;

    }

My WebApiConfig looks like this: 我的WebApiConfig看起来像这样:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        var cors = new EnableCorsAttribute("*", "*", "*");
        config.EnableCors(cors);
        config.MapHttpAttributeRoutes();
        GlobalConfiguration.Configuration.Filters.Add(new ValidateModelAttribute());
        //config.Routes.MapHttpRoute(
        //    name: "DefaultApi",
        //    routeTemplate: "{controller}/{id}",
        //    defaults: new { id = RouteParameter.Optional }
        //);
    }
}

When I run the project via visual studio, and go to the link http://localhost:53722/ping I get an unauthorized response which is what I expect. 当我通过Visual Studio运行该项目,并转到链接http:// localhost:53722 / ping时,我得到了我所期望的未经授权的响应。

When I put the website on IIS in an applicaion with the virtual path "/Account" I get a 404 response and it is trying to use the StaticFile Handler. 当我将网站放在带有虚拟路径“ / Account”的应用程序中的IIS上时,我收到404响应,它正在尝试使用StaticFile处理程序。

Through searching online I have runAllManagedModulesForAllRequests set to true. 通过在线搜索,我将runAllManagedModulesForAllRequests设置为true。 I have also found Configure ASP.NET Web API application as a Virtual Directory under ASP.NET MVC application which seems like the "/Account/" should not be interfering with the route. 我还发现将ASP.NET Web API应用程序配置为ASP.NET MVC应用程序下的虚拟目录 ,似乎“ / Account /”不应干扰路由。

I can not put the API at the route directory because I will be running multiple microservices under the same site. 我无法将API放在路由目录中,因为我将在同一站点下运行多个微服务。

How would I get the route to work using the virtual directory? 我将如何使用虚拟目录来工作?

Thanks, Steven 谢谢,史蒂文

Each site should be running as an Application and not a Virtual Directory (and ideally with each having its own application pool). 每个站点都应作为应用程序而不是虚拟目录运行(理想情况下,每个站点都有自己的应用程序池)。

Out of the box you would normally go for 开箱即用

 [Route("ping")]

This would give you a url of http://blah.com/ping If you are running this api in a sub folder/application then it maybe something like http://blah.com/subapp/ping 这将为您提供一个http://blah.com/ping的url如果您在子文件夹/应用程序中运行此api,则可能类似于http://blah.com/subapp/ping

This is all i have... 这就是我所拥有的...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM