我做错了什么?正在尝试从API获取响应
本文关键字:API 获取 响应 错了 什么 | 更新日期: 2023-09-27 18:20:55
我正在尝试从API获取任何响应。我读了很多书,但无论如何都会得到404(找不到资源)这是我的代码
Web.config:
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" defaultUrl="~/Home/Index">
</forms>
</authentication>
Global.asax.cs
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
GlobalConfiguration.Configure(WebApiConfig.Register);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
WebApiConfig.Register(GlobalConfiguration.Configuration);
}
WebApiConfig.cs
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
// webapi return json
// http://stackoverflow.com/questions/9847564/how-do-i-get-asp-net-web-api-to-return-json-instead-of-xml-using-chrome
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
}
ApiController:
public class MyAppApiController : ApiController
{
public HttpResponseMessage Test()
{
return Request.CreateResponse(HttpStatusCode.OK, new { Success = true, bbb = "assssdfv[spld[ssadf[ps" });
}
public HttpResponseMessage Login(LoginViewModel loginVM)
{
var url = this.Url.Link("Default", new { Controller = "AccountController", Action = "Login", model = loginVM});
return Request.CreateResponse(HttpStatusCode.OK, new { Success = true, RedirectUrl = url });
}
}
我正在使用Google Chrome Simple REST客户端进行测试:
- 以管理员身份运行我的VS
- 调试我的应用程序(localhost:12345与我的网站一起打开)
粘贴在谷歌Chrome简单REST客户端:
http://localhost:12345/api/Test
并运行GET=>结果404(未找到资源)
粘贴在谷歌Chrome简单REST客户端:
http://localhost:12345/api/Login
字段数据和标头无效,并运行POST=>结果404(未找到资源)
我的代码哪里有错?
更新:
我已更新:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
GlobalConfiguration.Configure(WebApiConfig.Register);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
//removed this: WebApiConfig.Register(GlobalConfiguration.Configuration);
}
然后我的代码是
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
作品但我仍然得到404错误-但错误是新的:{"Message":"No HTTP resource was found that matches the request URI 'http://localhost:12345/api/Login'.","MessageDetail":"No type was found that matches the controller named 'Login'."}
我的控制器名称是
myProjApiController :ApiController
但是
http://localhost:12345/api/myProjApiController/Login'
结果:
404 No type was found that matches the controller named 'myProjApiController'
您得到的是404,因为路由没有配置,并且当您取消注释路由配置时可能会得到错误,因为该代码被调用了两次,因此试图定义两次具有相同名称的路由。
您已经说过,在项目范围内搜索"DefaultApi"只会在WebApiConfig.cs中返回结果。请尝试对"WebApiCnfig"进行相同的搜索,以查找对它的多个调用。
或者,看看这些人有同样的问题(重复路线):
- 一条名为";x〃;已在路由集合中。路由名称必须唯一。ASP.NET MVC 3出现异常
- 一条名为';默认路由';已在路由集合中。路线名称必须唯一
- 一条名为';DefaultApi';已在路由集合中