I'm using .NETFramework v4.7.2
and i want to manage my Global.asax.cs
to change the behavior of my website. However I have difficulty understanding each line :
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) Someone has a brief explanation of what it does?
- (2) It lets
MvcApplication
displays the errors page (like 404, 500, ...) - (3) It ignores all path with
.axd
extension ? - (4) It creates a default page and call
HomeController.cs
and.cshtml
to display something ? How to change to display only a simpleindex.html
? - (5) It creates the
api
and callnameController.cs
to receiveGET
and/orPOST
requests ?- How to configure the reception of a simple
/api/Login
request withPOST
? This doesn't work and gives only404 (Not Found)
or415 (Unsupported Media Type)
errors :
- How to configure the reception of a simple
. LoginController.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
}),
...
});