2011-10-27 24 views
-1

我正在開發一個ASP.NET MVC應用程序。我一直在使用默認的路由規則。我有一些使用這樣的代碼呈現表單的觀點:服務路徑的ASP.NET MVC表單操作

@using (Html.BeginForm("ForgotPassword", "Register", FormMethod.Post)) 

這一直工作正常。表單操作會發布到/myapp/register/forgotpassword,並且一切正常。

現在需要將一些服務端點添加到同一個應用程序中。所以我在默認路線上添加了一些新路線。路由設置現在看起來像:

//New rule 
RouteTable.Routes.Add(
    new ServiceRoute(
    "api/user", new MyCustomerServiceHostFactory(), 
    typeof(UserWebservice))); 

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

在我添加新規則後,我的所有表格都打破了。檢查HTML,我可以看到表單操作是/myapp/api/user?action=ForgotPassword&controller=Register',這是完全錯誤的。

所以我的問題是:如何在不破壞所有現有表單的情況下路由新服務?

對於獎勵積分:這裏發生了什麼?

+0

您是否嘗試更改映射的順序,將默認值設置爲高於服務的順序。 –

回答

0

嘗試使用下面,

//New rule 
RouteTable.Routes.Add(
    new ServiceRoute(
    "UserWebservice", new MyCustomerServiceHostFactory(), 
    typeof(UserWebservice))); 

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

我想改變的代碼路徑添加爲Reference鏈接應該工作。 另請查看此博客以創建Dynamic service routes

+0

代碼示例中的路由不起作用。但是,您鏈接的動態服務路線完成了這一訣竅。謝謝! –