Jos*_* M. 5 c# model-view-controller asp.net-mvc routing attributerouting
当我使用IIS Express调试/运行并浏览到时http://localhost:1234/People,IIS Express尝试浏览People目录而不是执行People路由,我收到403.14 HTTP错误.所以我StaticFile在Web.config中禁用了处理程序并刷新了.现在我收到404.4 HTTP错误:
我知道路由有效,因为如果我重命名RoutePrefix,例如PeopleTest,那么路由被执行,我得到了我期望的响应.
如何说服IIS/Express更喜欢MVC路由而不是静态文件/目录?
我正在使用属性路由; 相关代码如下:
Web.config文件
<system.webServer>
<modules>
<remove name="FormsAuthentication" />
</modules>
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<remove name="StaticFile"/>
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)
Global.asax中
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AutofacConfig.Configure();
Run Code Online (Sandbox Code Playgroud)
启动\ WebApiConfig
namespace MyApi.Startup {
public static class WebApiConfig {
public static void Register(HttpConfiguration config) {
config.MapHttpAttributeRoutes();
}
}
}
Run Code Online (Sandbox Code Playgroud)
人们\把PeopleController
namespace MyApi.People {
[RoutePrefix("People")]
public partial class PagesController : BaseController {
[Route]
[HttpGet]
[ResponseType(typeof(IEnumerable<Person>))]
public IHttpActionResult Get() { ... }
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,由于我使用的是属性路由,因此我使用的是非标准文件夹结构.例如,我没有Controllers/Models/Views文件夹,而是每个业务区域都有根文件夹(例如,~\People包含"People"业务区域的控制器/模型/等).
我试过的
ExtensionlessUrlHandler-Integrated-4.0.通过添加设置修复RouteExistingFiles = true:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.RouteExistingFiles = true;
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
}
}
Run Code Online (Sandbox Code Playgroud)
因此ASP.NET路由将处理所有路由:https://msdn.microsoft.com/en-us/library/system.web.routing.routecollection.routeexistingfiles(v = vs.110).aspx
| 归档时间: |
|
| 查看次数: |
563 次 |
| 最近记录: |