2012-11-16 106 views
1

如何處理URL是這樣的:ASP MVC 3自定義路線

{subdomain}.{domainname}/{areas}/{controller}/{action} 

例如:user1.contoso.com/Manage/User/View

我希望它是路線:

{area} = Manage 
{controller} = User 
{action} = View 
{username} = user1 // View action parameter 

任何幫助將是很大的讚賞:-)

回答

1

一般路線在文件中定義Global.asax在該方法的RegisterRoutes

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


     routes.MapRoute(
      "Export",               // Route name 
      "Export/{action}/{table}",          // URL with parameters 
      new { controller = "Export", action = "AsExcel", table = "" } // Parameter defaults 
     ); 

     routes.MapRoute(
      "Default",            // Route name 
      "{controller}/{action}/{id}",       // URL with parameters 
      new { controller = "Home", action = "Index", id = "" } // Parameter defaults 
     ); 
    } 

有了這樣上面可以使用單獨的路線爲ExportController定義使得默認動作是AsExcel

在路線的情況下,你有一個我不確定是否區域是MVC模式的一部分。

還有什麼值得注意的是,你可能從這個線程 ASP.Net MVC with complex routes - how to keep it "sane"?

+1

我不知道你怎麼能在{}子域名做你的描述得到一些好處。你可以舉一個具體的例子來處理url surya.google.com/export/file/worksheet,這樣'surya'作爲一個參數來處理嗎? –

+1

@dtryon應該在服務器而不是應用程序中使用URL重寫來處理AFAIK子域,因爲它實際上是將服務器任務重定向到正確的子域的任務。像這裏討論的東西http://forums.iis.net/t/1155754.aspx/1?Sub+Domain+Rewrite可能有助於按照需要的方式使用常規路由 –