2016-09-24 47 views
1

我有重定向到另一個控制器和應用程序中的操作的問題。重定向到ASP中的URL核心

我重定向操作是這樣的:

public IActionResult Redirect(NotificationItemViewModel model) 
    { 
     NotificationItemToggleReadCommand command = new NotificationItemToggleReadCommand(); 
     command.Id = new EntityId(model.Id); 
     command.MarkAsRead = true; 


     var result = CommandProcessor.Run(command); 
     RedirectResult redirectResult = new RedirectResult(model.Subject.Url,true); 
     return redirectResult; 
    } 

我model.Subject.Url例如在這種格式:Identity/User#/Details/b868b08c-b3ba-4f45-a7b6-deb02440d42f

它的面積/控制器/操作/ ID。 問題是,我RedirectResult重定向我:

notifications/Identity/User#/Details/b868b08c-b3ba-4f45-a7b6-deb02440d42f 

和路由不存在(控制器/區域/控制器/操作/ ID) 如何可以強制使用重定向切斷第一本控制器的名字嗎?

我的路線:

app.UseMvc(routes => 
     { 
      routes.MapRoute("areaRoute", "{area:exists}/{controller}/{action=Index}/{id?}"); 

      routes.MapRoute(
       name: "controllerActionRoute", 
       template: "{controller}/{action}", 
       defaults: new { controller = "Home", action = "Index" }, 
       constraints: null, 
       dataTokens: new { NameSpace = "default" }); 

      routes.MapRoute(
       name: "default", 
       template: "{controller=Home}/{action=Index}/{id?}"); 
     }); 
    } 

感謝您的幫助。

回答

3

如果我正確理解您的問題,那是因爲您在Notification區域(某些其他區域)並嘗試移出該區域。

爲此,您可以執行以下任一操作。

  1. 除了提供相對網址,請在RedirectResult中提供絕對網址。現在的問題是,當應用程序激發重定向它考慮相對網址,這是問題。

  2. 第二個解決方案是使用RedirectToActionResult而不是RedirectResult。

    RedirectToActionResult redirectResult = new RedirectToActionResult("Details", "User", new { @area = "Identity", @Id = "b868b08c-b3ba-4f45-a7b6-deb02440d42f" }); 
    return redirectResult; 
    

在上面我認爲區域名稱是「身份」和控制器的名字是「用戶」和行動是「詳細信息」。 Acition有一個名稱爲「Id」的參數。您可以根據您的要求進行更改。在這裏,你不必構建絕對網址。

更新1

如果我認爲你總是要做的根,而忽略「通知」在begining。

至少你可以提供這樣的網址。

~/Identity/User#/Details/b868b08c-b3ba-4f45-a7b6-deb02440d42f 
+0

感謝您的回答。不幸的是,我沒有可能得到一個absoule網址。我總是得到這個相對的,這個URL可以指向其他的動作和控制器,所以我不能使用第二種解決方案。 – Ridikk12

+0

請參閱我的更新,也不明白「用戶#」。這可能會造成問題。 – dotnetstep