C# ASP.NET框架下的简单POST
这是我的问题,我想通过C# ASP.NET框架下的简单POST,c#,ajax,asp.net-mvc,asp.net-web-api,C#,Ajax,Asp.net Mvc,Asp.net Web Api,这是我的问题,我想通过POST请求接收客户端的用户名和密码 代码看起来很简单,但不起作用 LoginController.cs: public class LoginController : ApiController { [HttpPost] [ActionName("Login")] [Route("api/{controller}")] public HttpResponseMessage Login([FromBody] LoginJson json)
POST
请求接收客户端的用户名和密码
代码看起来很简单,但不起作用
LoginController.cs
:
public class LoginController : ApiController
{
[HttpPost]
[ActionName("Login")]
[Route("api/{controller}")]
public HttpResponseMessage Login([FromBody] LoginJson json)
{
return Request.CreateResponse(HttpStatusCode.OK);
}
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalFilters.Filters.Add(new HandleErrorAttribute());
RouteTable.Routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
RouteTable.Routes.MapRoute(...)
GlobalConfiguration.Configuration.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}",
defaults: new {action = RouteParameter.Optional}
);
}
LoginJson.cs
表单:
public class LoginJson
{
public string Username { get; set; }
public string Password { get; set; }
}
使用jQuery
的ajax
请求,我不想更改url,因为我想为3个不同的控制器使用3个url/api/Login
、/api/Method1
和/api/Method2
:
$.ajax({
url: '/api/Login',
type: 'POST',
dataType: "json",
contentType: "application/json, charset=utf-8",
data: JSON.stringify({
Username: username,
Password: password,
}),
...
});
API的路径,在Global.asax.cs
中:
public class LoginController : ApiController
{
[HttpPost]
[ActionName("Login")]
[Route("api/{controller}")]
public HttpResponseMessage Login([FromBody] LoginJson json)
{
return Request.CreateResponse(HttpStatusCode.OK);
}
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalFilters.Filters.Add(new HandleErrorAttribute());
RouteTable.Routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
RouteTable.Routes.MapRoute(...)
GlobalConfiguration.Configuration.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}",
defaults: new {action = RouteParameter.Optional}
);
}
我收到错误404(未找到)
。
我将更改
Global.asax.cs
文件。没有api/login
你可以这样做:
[ActionName("api/login")]
public HttpResponseMessage Login([FromBody] LoginJson json)
{
return Request.CreateResponse(HttpStatusCode.OK);
}
您只调用控制器,没有任何操作,因为没有定义默认操作,您将得到404错误 在jQuery中,您可以执行以下操作:
url: '/api/Login/Login'
或通过放置以下标记来更改路由:
[Route("api/login")]
或者在RouteConfig.cs中,这应该在设置其他路由(包括通用路由)之前完成
routes.MapRoute("Login", "Login/{action}",
defaults: new { controller = "Login", action = "Login" });
我不想更改jQuery的url,
[ActionName(“api/login”)]
不起作用,您忘记了最后一个option@Rodrigue我已经在RouteConfig.cs中添加了如何添加它,您的意思是将默认控制器设置为Login
?这是个坏主意。稍后,它将在其他控制器
类中返回问题。如果我有3个不同的控制器;我只想将/api/Login
,/api/Action1
,/api/Action2
作为我的POST
请求的URL。无论如何,只有在执行/Login
操作时,才能将默认控制器设置为Login
,否则任何解决方案都无法工作。所以其他控制器的工作原理与它们相同。而且忘了提及,您必须在RouteConfig
中的其他路线之前添加该选项,否则通用路线将接管。抱歉,您的回答对我来说仍然不够,我无法获得比404(未找到)
更多的错误代码。我可以问你一个更完整的答案吗?ApiController
来自ASP.NET Web API。AJAX来自AJAX。请不要删除有用的标记。这似乎是属性路由的完美选择@梅森,你能帮我建议一下你的答案吗?我尝试了不同的可能性,如链接中的提示,但我仍然有404(未找到)
我提供的文档清楚地描述了如何设置属性路由。不起作用。我想为3个不同的控制器使用URL:/api/Login
、/api/Method1
和/api/Method2