MVC5根目录上的动态路由

时间:2016-01-03 20:13:33

标签: asp.net-mvc-5 url-routing

我想让以下网址以动态方式映射到HomeController =>带有段变量的索引......

www.site.com/one www.site.com/two

 public HomeController
    {
      [Route("{segment:string}")]//this wont work... 404
      public ActionResult Index(string segment) //one or two
      {
         return View();
      } 
    }

1 个答案:

答案 0 :(得分:0)

我想我设法做到了这样:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapMvcAttributeRoutes();

        // other routes
    }
}

[RoutePrefix("")]
public class HomeController : Controller
{

    [Route("{segment}")]
    public ActionResult test(string segment)
    {
        return View();
    }
}

并访问以下网址:

http://localhost/TestWebApplication/segment1 - >转到test操作 - >放segment = "segment1"

所以,请稍等:确保启用了属性路由并跳过字符串约束(我收到一个无法识别的错误)。