可以在asp.net MVC中创建这样的URL吗?
1.
http://url/Home/Function1?name=tomek -> http://url/my-name-tomek
or
http://url/Home/Function1?name=john -> http://url/my-name-john
2.
http://url/Home/Function2?name=tomek&address=london -> http://url/my-name-tomek/my-address-london
or
http://url/Home/Function2?name=john&address=york -> http://url/my-name-john/my-address-york
那不是'高级路由'。
RouteConfig
:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes(); // <- with this line you enable attribute routing
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
namespace TestingUrl.Controllers
{
[RoutePrefix("MyController")] // <- Put a special url name for the controller
public class AnotherController : Controller
{
[Route("MyFunction/{name}/{city}")] // <- A special name for the action and the two parameters
[HttpGet]
public ActionResult AnotherIndex(string name, string city)
{
ViewBag.Name = name;
ViewBag.City = city;
return View();
}
}
}