2016-05-21 9 views
0

所有 我想要呼叫的行動2路。例如拖車的方式 - 路由在asp.net的MVC

http://localhost:16800/Content1/1/text 

http://localhost:16800/Content1/1 

和我routeconfig是缺省路由。我從路由屬性中使用。

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

和我的控制器使用路由屬性,那麼:

namespace WebApplication2.Controllers 

{

[RoutePrefix("Content1")] 
[Route("{action=Index}")] 
public class Content1Controller : Controller 
{ 

    [Route("{id}/{text}")] 
    public ActionResult Index(int id, string text) 
    { 
     return View(); 
    } 
} 

} 現在,這種方式爲我工作。 http://localhost:16800/Content1/1/text

這種方式不適合我。 http://localhost:16800/Content1/1 我只使用這兩種方式的打電話給我的行動。 提醒我的我的控制器我不知道,噸需要在我的網址定義動作的名稱上使用[Route("{action=Index}")]

回答

1
Try this, It will work(tested). 


namespace WebApplicationExamples.Controllers 
    { 
     [RoutePrefix("Content1")] 
     [Route("{action=Index}")] 
     public class Content1Controller : Controller 
     { 

      // GET: Content1 
      [Route("{id}")] 
      [Route("{id}/{text}")] 
      public ActionResult Index() 
      { 
       return View(); 
      } 
     } 
    } 
+0

我在測試項目來測試你的代碼。這工作很好。但它不適合我的主要項目。 :( –

+0

OK,你能告訴我哪個URL是不工作... –

+0

是這個網址,而不要爲我工作的http:// ...本地主機:16800 /內容1/1 –