gpt4 book ai didi

asp.net-mvc - 将属性路由添加到现有 MVC 5 Web 应用程序时,什么会导致属性路由不起作用?

转载 作者:行者123 更新时间:2023-12-02 01:48:04 25 4
gpt4 key购买 nike

我添加了 Web API nuget 和通过 Global.asax 调用 MapHttpAttributeRoutes 的 StartUp WebApiConfig 代码,但所有属性路由都不起作用。

protected void Application_Start()
{
SqlServerTypes.Utilities.LoadNativeAssemblies(Server.MapPath("~/bin"));

MvcHandler.DisableMvcResponseHeader = true;

AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
}

这是安装的包。

<packages>
<package id="EntityFramework" version="6.1.1" targetFramework="net451" />
<package id="Microsoft.AspNet.Identity.Core" version="2.0.1" targetFramework="net451" />
<package id="Microsoft.AspNet.Identity.EntityFramework" version="2.0.1" targetFramework="net451" />
<package id="Microsoft.AspNet.Identity.Owin" version="2.0.1" targetFramework="net451" />
<package id="Microsoft.AspNet.Mvc" version="5.1.2" targetFramework="net451" />
<package id="Microsoft.AspNet.Razor" version="3.1.2" targetFramework="net451" />
<package id="Microsoft.AspNet.WebApi" version="5.1.2" targetFramework="net451" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.1.2" targetFramework="net451" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.1.2" targetFramework="net451" />
<package id="Microsoft.AspNet.WebApi.Owin" version="5.1.2" targetFramework="net451" />
<package id="Microsoft.AspNet.WebApi.WebHost" version="5.1.2" targetFramework="net451" />
<package id="Microsoft.AspNet.WebPages" version="3.1.2" targetFramework="net451" />
<package id="Microsoft.AspNet.WebPages.Data" version="3.1.2" targetFramework="net451" />
<package id="Microsoft.AspNet.WebPages.WebData" version="3.1.2" targetFramework="net451" />
<package id="Microsoft.Data.Edm" version="5.6.1" targetFramework="net451" />
<package id="Microsoft.Data.OData" version="5.6.1" targetFramework="net451" />
<package id="Microsoft.Data.Services.Client" version="5.6.1" targetFramework="net451" />
<package id="Microsoft.Owin" version="2.1.0" targetFramework="net451" />
<package id="Microsoft.Owin.Host.SystemWeb" version="2.1.0" targetFramework="net451" />
<package id="Microsoft.Owin.Security" version="2.1.0" targetFramework="net451" />
<package id="Microsoft.Owin.Security.Cookies" version="2.1.0" targetFramework="net451" />
<package id="Microsoft.Owin.Security.OAuth" version="2.1.0" targetFramework="net451" />
<package id="Microsoft.SqlServer.Types" version="11.0.1" targetFramework="net451" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" />
<package id="Microsoft.WindowsAzure.ConfigurationManager" version="2.0.3" targetFramework="net451" />
<package id="Newtonsoft.Json" version="6.0.3" targetFramework="net451" />
<package id="Owin" version="1.0" targetFramework="net451" />
<package id="RestSharp" version="104.4.0" targetFramework="net451" />
<package id="SharpZipLib" version="0.86.0" targetFramework="net451" />
<package id="System.Spatial" version="5.6.1" targetFramework="net451" />
<package id="WindowsAzure.Storage" version="4.0.1" targetFramework="net451" />
</packages>

Controller 代码

[Authorize(Roles = "Admin")]
[RoutePrefix("admin/api/v/{vid}/photos")]
public class PhotosController : ApiController
{
[AllowAnonymous]
[Route("test", Name="Test")]
[HttpGet]
public IHttpActionResult Test(string vid)
{
return Ok();
}
}

最佳答案

我知道发生了什么,我有两个同名的 Api Controller 。看起来 Web Api 属性路由不会像 MVC 那样区分具有 namespace 约束的操作和 Controller 。我只是将 Admin 添加到控件类名称的前面,现在一切似乎都正常了。

关于asp.net-mvc - 将属性路由添加到现有 MVC 5 Web 应用程序时,什么会导致属性路由不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24375018/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com