简体   繁体   English

当MVC应用不是根站点时尝试生成返回URL

[英]Trying to generate a return url when mvc app is not the root site

I have a controller that generates a return url and then supplys it to the view for a javascript redirect. 我有一个控制器,该控制器生成一个返回URL,然后将其提供给视图以进行JavaScript重定向。 The problem is on my live site, the application is not the root site so I get some strange results from UriBuilder or when using Response.UrlRefferrer. 问题出在我的实时站点上,该应用程序不是根站点,因此从UriBuilder或使用Response.UrlRefferrer时会得到一些奇怪的结果。 Here's what I've finally got to sort of work 这就是我最终要做的工作

    [AllowAnonymous]
    [DisableCache]
    public ActionResult Login()
    {
        UriBuilder uriBuilder = null;

        if (Request.UrlReferrer != null)
        {
           uriBuilder = new UriBuilder(Request.UrlReferrer);
        }
        else
        {
            uriBuilder = new UriBuilder("prodsite");
        }
        //uriBuilder.Path += path;
        ViewBag.ReturnUrl = uriBuilder.Uri.ToString();
        return View();
    }

This solution works when the user uses a direct link to the controller, but if, say the user bookmarks this page and returns to it through their browser's link, the url becomes the root of the site. 当用户使用到控制器的直接链接时,此解决方案有效。但是,例如,如果用户对该页面添加了书签并通过浏览器的链接返回到该页面,则该URL成为站点的根目录。 I really wish I had a more MVCish way to handle the urls, but nothing so far has worked. 我真的希望我有一个更多的MVCish方法来处理URL,但是到目前为止没有任何效果。 Any suggestions? 有什么建议么? Also, I'm looking for a way to make this work on both live and test sites if that's at all possible so I don't have to go in and manually change code around. 另外,如果可能的话,我正在寻找一种方法在现场和测试站点上都可以做到这一点,因此我不必手动更改代码。 Thanks! 谢谢!

have you tried using Url.Action? 您是否尝试过使用Url.Action? In our javascript we use 在我们的JavaScript中,我们使用

window.location = '@Url.Action("Action", "Controller")';

and the links work local and on production. 链接在本地和生产环境中均有效。 Hopefully this helps. 希望这会有所帮助。

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

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