Sun*_*xit 61 c# asp.net asp.net-mvc asp.net-web-api
我有一个在v4.0集成模式下运行的asp.net Web表单应用程序.
我试图在App_Code文件夹中添加一个apicontroller.
在Global.asax中,我添加了以下代码
RouteTable.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = System.Web.Http.RouteParameter.Optional }
);
Run Code Online (Sandbox Code Playgroud)
当我尝试导航到控制器时http://localhost/api/Value,我收到404错误.
无扩展URL在处理程序部分中配置.我为网站启用了表单和匿名身份验证.
ExtensionLess url配置为'*.'
当我点击控制器的url时,请求由StaticHandler而不是ExtensionlessUrlHandler-Integrated-4.0处理.
我现在不知道为什么系统会抛出错误,如下图所示.

sha*_*non 111
我遇到了这个问题.
我尝试编辑我的,WebApiConfig.cs以满足这里的一些建议,并在其他地方编写代码示例.有些工作,但它没有解释为什么路线不能正常WebApiConfig.cs编码完全根据MS模板WebApi项目编码.
我的实际问题是,在手动添加WebApi到我的项目时,我没有按照配置调用的库存顺序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);
}
Run Code Online (Sandbox Code Playgroud)
我可以猜测为什么会这样,但我没有进一步调查.至少可以说是不直观的.
Alb*_*orz 32
问题出在您的路由配置中.Mvc路由与WebApi路由不同.
添加参考System.Web.Http.dll,System.Web.Http.Webhost.dll并System.Net.Http.dll然后配置你的API路由如下:
GlobalConfiguration.Configuration.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = System.Web.Http.RouteParameter.Optional }
);
Run Code Online (Sandbox Code Playgroud)
Bin*_*ose 17
确保以下事项
1.)如果您的web api是IIS中的4.5安装4.5,请确保您的IIS配置了.NET 4.5或4.0
使用管理员权限在命令提示符下运行此命令
C:\Windows\Microsoft.NET\Framework\v4.0.30319>aspnet_regiis.exe -i
Run Code Online (Sandbox Code Playgroud)
2.)将您的路由更改为
RouteTable.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "{controller}/{id}",
defaults: new { id = System.Web.Http.RouteParameter.Optional }
);
Run Code Online (Sandbox Code Playgroud)
并使用Demo/Get(其中demo是您的控制器名称)发出请求
如果1,2不工作尝试3
3.)在web.config文件中添加以下配置
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
Run Code Online (Sandbox Code Playgroud)
尝试仅使用Value控制器名称的一部分,如下所示:
http://localhost/api/Value
Run Code Online (Sandbox Code Playgroud)
注意:按照约定,路由引擎将采用作为控制器名称传递的值并将该单词附加
Controller到其中。通过输入ValueControllerURI,您可以让路由引擎查找名为 的类ValueControllerController,但它没有找到该类。
| 归档时间: |
|
| 查看次数: |
84521 次 |
| 最近记录: |