简体   繁体   English

如何重定向到MVC网站我的父母行动?

[英]How do I redirect to my parent action in MVC site?

I have been looking at several pages on here already such as: 我已经在这里查看过几页,例如:

How do I redirect to the previous action in ASP.NET MVC? 如何重定向到ASP.NET MVC中的上一个操作?

How can I redirect my action to the root of the web site? 如何将我的操作重定向到网站的根目录?

Along with several hours of searching google. 随着几个小时的搜索谷歌。

No where seems to have an answer to my problem and I am sure it should be possible within MVC somehow hence the reason I am now here to ask the question. 没有地方似乎可以解决我的问题,并且我确信在MVC内应该可以实现,因此我现在在这里提出问题的原因。

So the problem I am facing is that I want to allow the user to change the language of the page by choosing a new language from a drop down menu which is in its own partial view hence the problem, I need to redirect to the parent action and not the child. 所以我面临的问题是我想允许用户通过从下拉菜单中选择一种新的语言来更改页面的语言,该菜单在其自己的局部视图中,因此问题是,我需要重定向到父操作而不是孩子。 This all works fine as long as i send the user back to the root of the site. 只要我将用户送回网站的根目录,这一切都可以正常工作。 Using the following code: 使用以下代码:

  [HttpPost]
    public ActionResult RegionSelect(RegionSelectionModel model)
    {
        var currentUser = Session.GetCurrentUser();
        var currentDbUser = Session.GetUserEntity(_dataAccessLayer);
        if (!ModelState.IsValid)
        {
            model.AvailableRegions = CacheHelpers.GetAvailableRegions<RegionView>(_dataAccessLayer, _cache).ToList();
            return PartialView("_RegionSelect", model);
        }

        var selectedRegion = UsersControllerHelpers.SetSelectedRegion(model, _dataAccessLayer, _cache, _website.Client);
        var uri = model.OriginalUrl;
        var routeInfo = new RouteHelpers(uri, HttpContext.Request.ApplicationPath);

        // Route Data  
        var routeData = routeInfo.RouteData;

        routeData.Values.Remove("language");

        var defaultClientLanguageCode = _website.Client.LanguagesSupported.FirstOrDefault().Code;
        if (currentDbUser.Language.CountryCode != selectedRegion.PrimaryLanguage.CountryCode)
        {
            //TODO: Decide where to redirect or whether to refresh the whole page...
            if ((defaultClientLanguageCode == selectedRegion.PrimaryLanguage.CountryCode) || (model.SelectedRegionId == 0))
            {
                UsersControllerHelpers.UpdateUsersRegions(currentUser, selectedRegion, _website.Client, _cache, _dataAccessLayer,
                                                          Session);
                return RedirectToRoute(routeData.Values);
            }

            routeData.Values.Add("language",selectedRegion.PrimaryLanguage.CountryCode);
            return RedirectToRoute(routeData.Values);
        }

        return RedirectToRoute(routeData.Values);
    }

Two of my return statements return to the root page and one returns to the root but with a language so it would be " http://mysite/en-En/ " but what if the user is on a page other than the root site? 我的两个return语句返回到根页面,一个返回到根,但是使用某种语言,因此它将是“ http:// mysite / en-En / ”,但是如果用户不在根站点上,则该怎么办? ? I want to somehow redirect them back to this same action but with the correct language string at the start. 我想以某种方式将它们重定向回相同的操作,但开始时要使用正确的语言字符串。

How can i do this? 我怎样才能做到这一点?

I have thought of several "hacky" ways of doing this, such as splitting the URL and swapping the language codes over. 我想到了几种“ hacky”方式,例如拆分URL和交换语言代码。 But ideally I am looking to do this as clean as possible. 但理想情况下,我希望做到这一点尽可能干净。

Can anyone give me any idea's? 谁能给我任何想法? Or is it just not possible? 还是只是不可能?

It seems like it should be really simple but apparently not. 看起来应该真的很简单,但显然不是。

Thanks in advance for any help that you can provide. 在此先感谢您可以提供的任何帮助。

EDITED 已编辑

Added new code that is using code from suggested answer below. 添加了使用下面建议答案中的代码的新代码。

I am now having two new problems. 我现在有两个新问题。

  1. I am getting this error message, if there are any things in the URL such as ?page=1: A potentially dangerous Request.Path value was detected from the client (?) 如果URL中有任何内容(例如?page = 1),我将收到此错误消息:从客户端(?)检测到潜在的危险Request.Path值。

  2. If i try and remove the language completely using .Remove(). 如果我尝试使用.Remove()完全删除语言。 It removes it fine but when i try and redirect to the page in the default language it adds language?=language to the end of the URI. 它可以很好地删除它,但是当我尝试以默认语言重定向到该页面时,它将在URI末尾添加language?= language。

Any ideas how i can resolve these two issues? 有什么想法可以解决这两个问题吗?

This option is definitely my answer. 这个选项绝对是我的答案。 Leave me a comment if you need me to drop some code, and I can do that, but the examples on the linked website should get you started. 给我留言,如果你需要我放弃了一些代码,我能做到这一点,但链接的网站上的例子应该让你开始。

Use this method to change Request.UrlReferrer into Route data, then merge your language into that, then do a RedirectToRoute with the modified Route data. 使用此方法可以将Request.UrlReferrer更改为Route数据,然后将您的语言合并到该数据中,然后使用修改后的Route数据执行RedirectToRoute。

Just use RouteData.Values.Add, RouteData.Values.Remove, and RouteData.values["whatever"], then pass that modified RouteData.Values object to RedirectToRoute() 只要使用RouteData.Values.Add,RouteData.Values.Remove和RouteData.values [ “无所谓”],然后传递改性RouteData.Values反对RedirectToRoute()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM