【发布时间】:2019-03-31 04:02:43
【问题描述】:
我正在使用.NETFramework v4.7.2,我想管理我的Global.asax.cs 来改变我的网站的行为。但是我很难理解每一行:
public class MvcApplication : HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas(); // (1)
GlobalFilters.Filters.Add(new HandleErrorAttribute()); // (2)
RouteTable.Routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); // (3)
RouteTable.Routes.MapRoute( // (4)
"Default",
"{controller}/{action}/{id}",
new {controller = "Home", action = "Index", id = UrlParameter.Optional}
);
GlobalConfiguration.Configuration.Routes.MapHttpRoute( // (5)
name: "DefaultApi",
routeTemplate: "api/{controller}"
);
}
}
- (1) 有人简要解释了它的作用吗?
- (2) 它让
MvcApplication显示错误页面(如404、500、...) - (3) 忽略所有带有
.axd扩展名的路径? - (4) 它创建一个默认页面并调用
HomeController.cs和.cshtml来显示一些东西?如何更改为只显示一个简单的index.html? - (5) 它创建
api并调用nameController.cs来接收GET和/或POST请求?- 如何使用
POST配置简单的/api/Login请求的接收?这不起作用,只给出404 (Not Found)或415 (Unsupported Media Type)错误:
- 如何使用
。 登录控制器.cs:
[HttpPost]
[ActionName("Login")]
[Route("api/[controller]")]
public HttpResponseMessage LoginPost([FromBody] LoginJson json)
{
return Request.CreateResponse(HttpStatusCode.OK);
}
LoginJson.cs:
public class LoginJson
{
public string Username { get; set; }
public string Password { get; set; }
}
jQuery:
$.ajax({
url: '/api/Login',
type: 'POST',
dataType: "json",
contentType: "application/json, charset=utf-8",
data: JSON.stringify({
Username: username,
Password: password
}),
...
});
【问题讨论】:
标签: c# asp.net .net asp.net-mvc