Action method has following restrictions Action method must be public It cannot

Action method has following restrictions action

This preview shows page 54 - 63 out of 88 pages.

Action method has following restrictions. - Action method must be public. It cannot be private or protected. - Action method cannot be overloaded. - Action method cannot be a static method. ActionResult is a base class of all the result type which returns from Action method. Base Controller class contains methods that returns appropriate result type e.g. View(), Content(), File(), JavaScript() etc. Action method can include primitive data type, complex type or Nullable type parameters.
using Microsoft.AspNetCore.Mvc; namespace PartyInvites.Controllers { public class HomeController : Controller { public string Index() { return “Hello World; } } } Once you run the project with these changes, Browser will make an HTTP request to the server. The request will be handled by the action method Index. The result will be sent back to the browser.
Understanding the Routes URL Routes: MVC applications use the ASP.NET routing system , which decides how URLs map to controllers and actions. Route: is a rule that is used to decide how a request is handled. Default routes if you use templates in the project creation will be / /Home /Home/Index The "Hello World" output wasn’t an HTML. To properly produce HTML response to a request we need “view”, which tells MVC to produce a response to a request. Lets change our HomeController code again
Creating and Rendering a View using Microsoft.AspNetCore.Mvc; namespace PartyInvites.Controllers { public class HomeController : Controller { public ViewResult Index() { return View("MyView"); } } } Returning a ViewResult object meaning the view is going to be render. Running the application at this point will cause an error, as “ MyView ” has not yet be created .
Views are stored in the Views folder , organized into subfolders. Views that are associated with the Home controller, for example, are stored in a folder called Views/Home. Views that are not specific to a single controller are stored in a folder called Views/Shared. Visual Studio creates the Home and Shared folders automatically when the Web Application (Model-View-Controller) template is used.
Creating a View
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Index</title> </head> <body> <div> Hello World (from the view) </div> </body> </html> @{ Layout = null;}, this expression is a Razor code , which will be interpreted by a Razor view engine. Razor view Engine (more in coming classes): process the contents of views and generates and HTML
Now that the Index method returns a ViewResult, MVC renders a view and returns the HTML it produces. I told MVC which view should be used, so it used the naming convention to find it automatically. The convention is that the view has the name of the action method and is contained in a folder named after the controller: /Views/Home/MyView.cshtml . We can also return other results from action methods besides strings “Hello World” and ViewResult objects.

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture