How to do it...

  1. Add another folder to your application called Controllers:
  2. Inside the Controllers folder, add a new class called StudentController. Inside the StudentController, add a method called Find(). When you are done, your class will look as follows:
        public class StudentController
{
public string Find()
{
return "Found students";
}
}
  1. Back in the Startup class, add a private void method called FindController() that takes a parameter of type IRouteBuilder. Make sure that you also add the using Microsoft.AspNetCore.Routing; namespace to your class. Your method should look as follows:
        private void FindController(IRouteBuilder route)
{

}
  1. In the Configure() method, change the app.UseMvc(); to app.UseMvc(FindController);.
  2. We now need to tell our application how to look at a URL to determine which Controller to call. We will use convention-based routing here which uses a template that we define, to determine which Controller to call. Consider the following template {controller}/{action}. Our application will then use this template to split apart a URL and identify which part of a URL is the Controller part and which part of the URL is the Action. Using our StudentController class, the method Find() is the Action the template refers to. Therefore, when the application receives an incoming HTTP request with a URL /Student/Find, it will know to look for the StudentController class and go to the Find() method inside that Controller.
We do not need to have the URL explicitly named /StudentController/Find because the MVC framework will, by convention, automatically take the word Student in the {controller} portion of the template and apply Controller to it to identify the name of the Controller to find.
  1. Add the route mapping to the FindController() method. This tells the application that the template name is default and the template needs to look for a {controller}/{action} template in the URL. Your code should now look as follows:
        private void FindController(IRouteBuilder route)
{
route.MapRoute("Default", "{controller}/{action}");
}
  1. Putting it all together, your Startup class will look as follows:
        public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}

public void Configure(IApplicationBuilder app,
IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole();

if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
DefaultFilesOptions options = new DefaultFilesOptions();
options.DefaultFileNames.Add("friendlyError.html");
app.UseDefaultFiles(options);

app.UseExceptionHandler("/friendlyError");
}

app.UseStaticFiles();
app.UseMvc(FindController);
}

private void FindController(IRouteBuilder route)
{
route.MapRoute("Default", "{controller}/{action}");
}
  1. Save your code and enter the following to the end of your URL in your browser: /student/find. My URL looks as follows, but yours will differ because the port number will most likely be different to mine: http://localhost:25860/student/find. Entering that in your browser will route the incoming HTTP request to the correct Controller.
  1. What should we do, however, if the URL is not in the correct format or the Controller can't be found. Well, this is where we can add defaults to our template. Remove the /student/find portion of the URL and hit enter. You should now see an error 404 in the browser. This is because the application could not find the Controller based on our URL. Add another class to our Controllers folder. Call this class ErrorController. Then, create a method inside this controller called Support(). Your code should look as follows:
        public class ErrorController
{
public string Support()
{
return "Content not found. Contact Support";
}
}
  1. Back in the Startup class, modify the template in the FindController() method. It should look as follows:
        route.MapRoute("Default", "{controller=Error}/{action=Support}");
  1. What this does is tell our application that if it does not find a Controller, it should default to the ErrorController class and execute the Support() method inside that class. Save your code and refresh your browser to see the application default to the ErrorController.
  1. As you can see, the routing in ASP.NET MVC is very flexible. The previous steps listed discussed what we call convention-based routing. There is another routing method called attribute-based routing that uses attributes on our Controllers. Go to the ErrorController class and add the following using Microsoft.AspNetCore.Mvc; namespace to the class. Then, add an attribute [Route("Error")] to the class name and an attribute [Route("Support")] to your method. Your code should look as follows:
        [Route("Error")]
public class ErrorController
{
[Route("Support")]
public string Support()
{
return "Content not found. Contact Support";
}
}
  1. In the Startup class inside the FindController() method, comment out the line route.MapRoute("Default", "{controller=Error}/{action=Support}");. In the browser, add the text /Error/Support to the end of your URL and enter. You will see that the application correctly matches the ErrorController, based on the attributes defined inside the ErrorController class.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.69.163