Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 使用MvcContrib';s强类型重定向到带有锚标记(hashtag)的操作?_Asp.net Mvc 3_Mvccontrib - Fatal编程技术网

Asp.net mvc 3 使用MvcContrib';s强类型重定向到带有锚标记(hashtag)的操作?

Asp.net mvc 3 使用MvcContrib';s强类型重定向到带有锚标记(hashtag)的操作?,asp.net-mvc-3,mvccontrib,Asp.net Mvc 3,Mvccontrib,我使用MvcContrib的强类型RedirectToAction()从一个控制器操作重定向到另一个控制器操作,同时避免在应用程序流中使用魔术字符串,如下所示: this.RedirectToAction<FooController>(c => c.Bar()); /foo/bar/#yarrr 。。。但现在我希望能够重定向到一个URL,并在其末尾添加锚/标签,然后将窗口滚动到标签,如下所示: this.RedirectToAction<FooController

我使用MvcContrib的强类型RedirectToAction()从一个控制器操作重定向到另一个控制器操作,同时避免在应用程序流中使用魔术字符串,如下所示:

this.RedirectToAction<FooController>(c => c.Bar());
/foo/bar/#yarrr  
。。。但现在我希望能够重定向到一个URL,并在其末尾添加锚/标签,然后将窗口滚动到
标签,如下所示:

this.RedirectToAction<FooController>(c => c.Bar());
/foo/bar/#yarrr  
我可以将hashtag放在TempData[]中,将其写在javascript变量中,并让窗口通过javascript滚动——但我宁愿遵循惯例,将hashtag作为URL的结尾

有什么想法或自制的解决方案吗?
MvcContrib似乎不支持它。

我不知道MvcContrib中是否存在这样一个ActionLink重载,但编写一个会很简单:

using System;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Routing;

public static class HtmlExtensions
{
    public static IHtmlString ActionLink<TController>(
        this HtmlHelper html,
        Expression<Action<TController>> action,
        string linkText,
        object htmlAttributes,
        string fragment
    ) where TController : Controller
    {
        var routeValues = Microsoft.Web.Mvc.Internal.ExpressionHelper
            .GetRouteValuesFromExpression(action);
        return html.RouteLink(
            linkText: linkText,
            routeName: null,
            protocol: null,
            hostName: null,
            fragment: fragment,
            routeValues: routeValues,
            htmlAttributes: new RouteValueDictionary(htmlAttributes)
        );
    }
}
然后在控制器内部:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return this.RedirectToAction<FooController>(c => c.Bar(), "yarrr");
    }
}
公共类HomeController:控制器
{
公共行动结果索引()
{
返回这个.RedirectToAction(c=>c.Bar(),“yarrr”);
}
}

可能存在@nemesv的重复,不幸的是,重复的问题尚未得到回答。我回答了这个问题,因为这是我在这个主题上看到的第一个问题。MvcContrib的RedirectToAction用于从一个控制器操作重定向到另一个控制器操作-强类型。我正在寻找一种方法来做到这一点,只是还包括一个标签作为参数。。。而不是如何在视图中生成actionlink链接。如果不清楚的话,我很抱歉。对你来说有些琐碎的事情;在没有适当知识的情况下,这对我来说非常困难:)非常感谢-我学习了UrlHelper和ExpressionHelper来引导!可以为htmlAttributes的RedirectToAction编写重载,以允许打开新的浏览器选项卡。请看我的问题。谢谢@Joe,不行。这应该发生在客户机上。服务器不知道浏览器意味着什么,更不用说浏览器选项卡了。
Microsoft.Web.Mvc.Internal.ExpressionHelper
在MVC4中似乎不存在。有人在MVC4中找到了解决方案吗?
public class HomeController : Controller
{
    public ActionResult Index()
    {
        return this.RedirectToAction<FooController>(c => c.Bar(), "yarrr");
    }
}