-1

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 simple index.html ?
  • (5) It creates the api and call nameController.cs to receive GET and/or POST requests ?
    • How to configure the reception of a simple /api/Login request with POST ? This doesn't work and gives only 404 (Not Found) or415 (Unsupported Media Type) errors :

. 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
        }),
        ...
});
Rod
  • 712
  • 2
  • 12
  • 36

1 Answers1

5

1) I think this will explain all about areas: https://exceptionnotfound.net/asp-net-mvc-demystified-areas/

2) Yes but it lets you customize what happens by default if error / exception occurs. For example you can set if something goes wrong to redirect to some other controller...

3) Answered here in comment: What is routes.IgnoreRoute("{resource}.axd/{*pathInfo}") - The reason for putting IgnoreRoute into the routing configuration of MVC is to ensure that MVC doesn't try to handle the request. This is because .axd endpoints need to be handled by another HTTP handler (a handler that is not part of MVC) in order to serve scripts.

4) No it is just setting a default way of reaching an action inside the controller... The action tells what to return (html or cshtml or ..) ...Return regular html for example like this:

public ActionResult Index()
{
    return Content("<html></html>");
}

5) Similar to 4) this is a default route for web API requests. Your API call is correct but the error you are getting means that the request you are sending to that API is wrong, see this question: Unsupported media type ASP.NET Core Web API

ivpavici
  • 1,117
  • 2
  • 19
  • 30
  • Thank you for your answers, please note the change for question (5) because I can't understand my mistake. Btw, the link refers to `.NET Core` problem, i'm with `.NET Framework` – Rod Oct 26 '18 at 13:14