sách gpt4 ai đã đi

c# - WebApi 2 Http 发布 405 "The requested resource does not support http method ' 发布'"

In lại 作者:行者123 更新时间:2023-11-30 17:37:11 31 4
mua khóa gpt4 Nike

我已经查看了所有 StackOverflow,但没有找到适合我的案例的解决方案我有 405 HttpStatusCode 调用 API/Regions/Create 操作这是我的 baseController 方法:

 [HttpPost]
public virtual IHttpActionResult Create([FromBody]T entity)
{
thử
{
repository.Create(entity);

return Ok(entity);
}
catch (HttpException e)
{
return new ExceptionResult(e, this);
}
}

区域 Controller .cs

public class RegionsController : BaseController
{
public RegionsController()
{ }
public RegionsController(RegionRepository _repository)
{
RegionRepository repository = new RegionRepository();//_repository;
}

RegionRepository repository = new RegionRepository();

[HttpGet]
[Route("api/regions/{name}")]
public IHttpActionResult Get(string name)
{
thử
{
var region = repository.Get(name);

return Ok(region);
}
catch (HttpException e)
{
return new ExceptionResult(e, this);
}
}
}

WebApi 配置:

      public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Configure Web API to use only bearer token authentication.
// Web API configuration and services
var cors = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(cors);


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 }
);
}
}
}

全局.asax:

    public class WebApiApplication : System.Web.HttpApplication
{
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
protected void Application_Start()
{
logger.Info("Application Start");
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);

//GlobalConfiguration.Configuration.MessageHandlers.Add(new CorsHandler());
}

//protected void Application_BeginRequest(object sender, EventArgs e)
//{
// HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
// //if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
// //{
// HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "POST, PUT, DELETE");

// HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
// HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
// HttpContext.Current.Response.End();
// // }
//}

private void Application_Error(object sender, EventArgs e)
{

var lastException = Server.GetLastError();

NLog.LogManager.GetCurrentClassLogger().Error(lastException);

}


}
}

和 Web.Config:

      
















有什么错误的建议吗?

1 Câu trả lời

您应该在 POST 请求上调用 API/Regions,而不是 API/Regions/Create,除非您在操作的 Route 属性中指定 API/Regions/Create。 WebApi 将知道要搜索什么方法来处理请求。

关于c# - WebApi 2 Http 发布 405 "The requested resource does not support http method ' 发布'",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38397370/

31 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com