ASP.NET MVC的WebAPI 404错误 [英] ASP.NET MVC WebAPI 404 error

查看:114
本文介绍了ASP.NET MVC的WebAPI 404错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在V4.0集成模式下运行的asp.net web窗体应用程序。

I have an asp.net web forms application running under v4.0 integrated mode.

我试图在APP_ code文件夹添加一个apicontroller。

I tried to add an apicontroller in the App_Code folder.

在Global.asax中,我添加了以下code

In the Global.asax, I added the following code

 RouteTable.Routes.MapHttpRoute(
      name: "DefaultApi",
      routeTemplate: "api/{controller}/{id}",
      defaults: new { id = System.Web.Http.RouteParameter.Optional }
 );

当我试图在 HTTP浏览到控制器://本地主机/ API /值,我得到的404错误

When I tried to navigate to the controller at http://localhost/api/Value, I get the 404 error.

该扩展名的URL在处理器部分配置。我对网站启用的形式和匿名身份验证。

The extensionless url is configured in the handler section. I have forms and anonymous authentication enabled for the website.

扩展名的URL被配置为*。

ExtensionLess url is configured for '*.'

当我打的网址为控制器,请求由StaticHandler,而不是处理ExtensionlessUrlHandler - 集成 - 4.0。

When I hit the url for controller, the request is handled by StaticHandler instead of ExtensionlessUrlHandler-Integrated-4.0.

我不知道现在为什么系统会抛出错误如下图所示的图像中。

I have no clue now why the system will throw the error as shown in the image below.

推荐答案

我遇到了这个问题。

我试图编辑我的 WebApiConfig.cs 来满足一些其他地方的这里的建议和code样品。有些工作,但是当 WebApiConfig.cs 是codeD完全一样按照MS模板的WebAPI项目并没有为何路线是行不通的解释。

I tried editing my WebApiConfig.cs to meet a number of recommendations here and code samples elsewhere. Some worked, but it didn't explain to why the route was not working when WebApiConfig.cs was coded exactly as per the MS template WebApi project.

我实际的问题是,在手动添加的WebAPI 来我的项目,我没有遵循从的Global.asax <股票订单配置通话/ code>

My actual problem was that in manually adding WebApi to my project, I had not followed the stock order of configuration calls from Global.asax

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        // This is where it "should" be
        GlobalConfiguration.Configure(WebApiConfig.Register);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        // The WebApi routes cannot be initialized here.
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }

我可以做,为什么这是猜测,但我没有进一步的调查。这不是直观的,至少可以说。

I could make guesses about why this is, but I didn't investigate further. It wasn't intuitive to say the least.

这篇关于ASP.NET MVC的WebAPI 404错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆