gpt4 book ai didi

asp.net-mvc-5 - web api 2区域路由

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

我正在使用 asp.net mvc 5 和 web api 2。对于 asp.net mvc 5 项目,我一切正常……但是新的我正在尝试添加 web api 2 路由……当我使用区域时。

我有 web api 2 Controller 在项目根目录下工作:

 //this is working 
namespace EtracsWeb.Controllers
{
public class TestController : ApiController
{
//localhost:port/api/test ...this is working
[HttpGet]
public HttpResponseMessage Get()
{

return new HttpResponseMessage()
{
Content = new StringContent("GET: Test message")
};
}

}
}

所以我假设我的 Global.asax , 我的 routeconfig.cs和我的 webapiconfig.cs是正确的......(未显示)......

但现在我正试图让我的区域中的 web api 2 工作......

我已经阅读了我可以在网上找到的所有内容,这似乎应该
工作:
namespace EtracsWeb.Areas.WorkOrder
{
public class WorkOrderAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "WorkOrder";
}
}

public override void RegisterArea(AreaRegistrationContext context)
{

context.Routes.MapHttpRoute(
name: "AuditModel_Api",
routeTemplate: "WorkOrder/api/AuditModelApi/{id}",
defaults: new { id = RouteParameter.Optional }
);

//default
context.Routes.MapHttpRoute(
name: "WorkOrder_Api",
routeTemplate: "WorkOrder/api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);

context.MapRoute(
"WorkOrder_default",
"WorkOrder/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
}
}

我的 Controller 代码是:
namespace EtracsWeb.Areas.WorkOrder.ApiControllers
{
public class AuditModelApiController : ApiController
{
IManufacturerStopOrderModelService auditModelService = new WorkOrder.Services.VWAuditModelService(UserSession.EtracsUserID, UserSession.ProgramID, UserSession.EtracsSessionID, UserSession.ConnectionString);

[HttpGet]
[Route("AuditModelApi")]
public HttpResponseMessage Get()
{

return new HttpResponseMessage()
{
Content = new StringContent("GET: Test message")
};
}

[Route("AuditModelApi/AuditModels")]
public IEnumerable<VwAuditModel1> GetAuditModels()
{
return auditModelService.GetModels();
}

public IHttpActionResult UpdateAuditMode()
{
//example of what can be returned ... NotFound, Ok ... look into uses...

VwAuditModel1 result = new VwAuditModel1();
return Ok(result);

return NotFound();
}
}
}

我已经尝试了带和不带属性命名的 Controller [Route] ...
我无法开始工作......

无论是简单的情况
    public HttpResponseMessage Get()

和“真实”案例
  public IEnumerable<VwAuditModel1> GetAuditModels()

返回相同的结果。从浏览器,使用
http://localhost:64167/WorkOrder/api/AuditModelApi


http://localhost:64167/WorkOrder/api/AuditModelApi/AuditModels

我得到以下信息:
<Error>
<Message>
No HTTP resource was found that matches the request URI 'http://localhost:64167/WorkOrder/api/AuditModelApi/AuditModels'.
</Message>
<MessageDetail>
No route providing a controller name was found to match request URI 'http://localhost:64167/WorkOrder/api/AuditModelApi/AuditModels'
</MessageDetail>
</Error>

最佳答案

您需要获取 HttpConfiguration来自 GlobalConfiguration 的实例对象并调用 MapHttpAttributeRoutes()来自 RegisterArea() 内部的方法AreaRegistration.cs的方法.

    public override void RegisterArea(AreaRegistrationContext context) 
{
GlobalConfiguration.Configuration.MapHttpAttributeRoutes();

//... omitted code
}

最后你必须在 WebApiConfig删除 config.MapHttpAttributes()方法,否则您将收到重复的异常。
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
config.EnableCors();

// Configure Web API to use only bearer token authentication.
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));


// Web API routes
//config.MapHttpAttributeRoutes();

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

关于asp.net-mvc-5 - web api 2区域路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29125412/

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