2015-06-11 25 views
3

參數缺少行動我下面這篇文章RedirectToAction with parameter出於某種原因做RedirectToAction上的URL

return RedirectToAction("index","service",new {groupid = service.GroupID}); 

,它返回的URL是不是有什麼期望。例如,它將返回http://localhost/appname/service?groupid=5而不是http://localhost/appname/service/index?groupid=5。有沒有辦法讓它返回預期的URL?

更新:RouteConfig.cs

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

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

感謝

+0

顯示自'RouteConfig.cs' –

+1

你的路由我無法重現您的問題(對我來說工作正常)。儘管我建議你將'ServiceController'中的'Index()'方法的參數改爲'int id'而不是'int groupid',並使用'return RedirectToAction(「index」,「service」,new {id = service。 GroupID});'所以它生成'../ Service/Index/5'而不是'../ Service/Index?groupid = 5'(或者用'url:「Service/Index/{groupid} 」 –

+0

@Ryios:羣ID是一個整數類型 –

回答

1

正在發生的事情是,你的默認路由被定義爲

url: "{controller}/{action}/{id}", 

Index()方法沒有一個參數指定id (它的groupId),所以路由引擎只使用{action}的默認值。您可以生成路由您想通過更改參數名稱id

public ActionResult Index(int id) 

,並在其他方法使用

RedirectToAction("Index","Service",new {id = service.GroupID}) 

或前添加一個新的路由定義默認路由

routes.MapRoute(
    name: "Service", 
    url: "Service/Index/{groupId}", 
    defaults: new { controller = "Service", action = "Index", groupId = UrlParameter.Optional } 
); 

注意在這兩種情況下,這將產生../Service/Index/5而不是../Service/Index?groupId=5 b如果你真的想要第二個選項,然後改變上面的路線只是url: "Service/Index",(省略最後一個參數)

1

好吧,我想通了,並在測試環境中轉載它。這是因爲路由。

在MVC當你使用一個通用的捕獲像你所有航線在這裏做:

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

作爲默認操作映射到控制器的根它將始終把指標。因此,localhost/somecontroller將始終調用index,並且url將在localhost/somecontroller或localhost/somecontroller/index處加載索引。

有2種方式來解決先從最簡單的

解決方案1這個問題:

在服務控制器,不命名你的方法指標,將其命名爲別的,像NotIndex,IDoStuff,隨你。只是這樣做會導致重定向重定向到Service/IDoStuff(w/e)。但是,執行此方法意味着localhost/appname/service將生成404(因爲缺省操作「Index」不存在)。

解決方案2:可以讓你保持行動名爲Index

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

方案2問題 指定嚴格的路線是這樣,傷了你的默認路由包羅萬象的,如果你把默認捕獲所有迴路由你回到原來的問題,因爲MVC將通過路由收集並將每個路由應用到url,直到找到匹配的匹配,匹配的第一個匹配的是它使用的匹配,如果找不到匹配的路由,那麼bam 404(找不到頁面/無資源)。

然而,像你我要嚴格的網址,而不會違約,所以我所做的是我用的解決方案2.

然後找回我的根URL加載首頁 - >索引我添加了一個重寫規則到我的網站的.config

<system.webServer> 
    <rewrite> 
    <rules> 
     <rule name="RootRedirect" stopProcessing="true"> 
     <match url="^$" /> 
     <action type="Redirect" url="/Home/Index/{R:0}" /> 
     </rule> 
    </rules> 
    </rewrite>  
</system.webServer> 

對於工作,你需要有UrlRewrite功能在IIS(安裝),使其在GAC /機器存在配置等

而且重新路由規則似乎是啓用永久重定向規則,因此瀏覽器將不會重定向到它一旦客戶端瀏覽器一次訪問該站點,就向服務器發出2個請求。

+0

@Stephen Muecke曾與ID的詳細信息= GROUPID的一部分,我覺得兩個答案都技術上是正確的。 –