简体   繁体   中英

skipping controller and action names mvc

I'm developing a project using ASP.NET MVC

im using ASP.NET Web Application with MVC enabled

I Have a Home controller which contains default Index action, there is a list of stuff that I can access like this

site.com/?parameter=test1

but i want to be able to access the list like this

site.com/test1

where test1 is the parameter and used as a search parameter.

i have tried the below examples, but none of them worked

 routes.MapRoute(
            name: "Default",
            url: "/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

and this

   routes.MapRoute(
            name: "Default",
            url: "{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

try using the [Route] attribute above an action and leave the defaults in your startup.

[Route("/{id?}"]
public IActionResult Index(string id)
{
  return View();
}

for net framework use below

    public ActionResult Index(string id)
    {
        return View();
    }

RouteConfig.cs

    routes.MapRoute(
   name: "Default",
   url: "{id}",
   defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
                );

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