简体   繁体   中英

MapRoute to specific action in Home controller

Inside my home controller I have two views, Index and About .

在此处输入图片说明

I'm trying to configure requests to example.com/about to hit the About page.

Inside my Startup.cs file I have the below MapRoute's

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");

            routes.MapRoute(
              "Id",
              "{id}",
              new { controller = "Home", action = "About" });
        });

This allow's requests to example.com/about to be passed into the About action. My problem is that it also captures all other requests after the forwardslash. For instance, requests to example.com/foobarr will also hit the About action.

How can I alter the MapRoute so that requests to example.com/about reach the About action, but anything else is ignored.

Isn't it better to configure routes inside controllers, specifying attributes above methods, for example:

 [HttpGet("Home/About")]

The custom attribute is for an action method for about route. In this case it is easier to handle specific routes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM