Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.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 实例化帮助器类,但不在视图中_Asp.net Mvc_Razor_Instantiation_Helper - Fatal编程技术网

Asp.net mvc 实例化帮助器类,但不在视图中

Asp.net mvc 实例化帮助器类,但不在视图中,asp.net-mvc,razor,instantiation,helper,Asp.net Mvc,Razor,Instantiation,Helper,我需要一种方法来实例化一个类,当涉及到视图中的链接时,该类应该可以帮助我进行一些querystring构建,我不能将我需要的这些方法用作静态方法,因为这将导致querystring构建器在应用程序的整个生命周期中保留数据(这将导致一些严重问题) 所以我想问你们的问题是, 有没有可能知道如何实例化我需要的类/对象,但不能在实际视图中实例化 因此,为了让问题保持简单。。我是否可以做一些类似的事情: @MyInstantiatedObject.DoStuff() 在我看来,在我看来,以前没有这样做过

我需要一种方法来实例化一个类,当涉及到视图中的链接时,该类应该可以帮助我进行一些querystring构建,我不能将我需要的这些方法用作静态方法,因为这将导致querystring构建器在应用程序的整个生命周期中保留数据(这将导致一些严重问题)

所以我想问你们的问题是, 有没有可能知道如何实例化我需要的类/对象,但不能在实际视图中实例化

因此,为了让问题保持简单。。我是否可以做一些类似的事情:

@MyInstantiatedObject.DoStuff()
在我看来,在我看来,以前没有这样做过:

@{
    var MyInstantiatedObject = new MyClass()

}
我确实知道我需要如何实例化对象,但我的问题是是否可以用其他方式(比如告诉web.config处理它..或者使用一些app_code@helper magic或其他东西)


提前谢谢

您试图实现的目标与MVC的理念背道而驰。如果要在操作之间保留查询字符串数据,可以创建自定义ActionLink html帮助程序,如下所示:

public static MvcHtmlString ActionLinkWithQueryString(this HtmlHelper htmlHelper, 
    string linkText, string actionName)
{
    var routeValueDictionary = new RouteValueDictionary();

    return htmlHelper.ActionLinkWithQueryString(linkText, 
        actionName, routeValueDictionary);
}

public static MvcHtmlString ActionLinkWithQueryString(this HtmlHelper htmlHelper, 
   string linkText, string actionName, RouteValueDictionary routeValues)
{
    var queryString = HttpContext.Current.Request.QueryString;

    if (queryString.Count > 0)
    {
        foreach (string key in queryString.Keys)
        {
            routeValues.Add(key, queryString[key]);
        }
    }

    return htmlHelper.ActionLink(linkText, actionName, routeValues);
}
private RedirectToRouteResult RedirectToActionWithQueryString(string actionName)
{
    var queryString = Request.QueryString;
    var routeValues = new RouteValueDictionary();

    foreach (string key in queryString.Keys)
    {
        routeValues.Add(key, queryString[key]);
    }

    return RedirectToAction(actionName, routeValues);
}
您还可以在控制器或控制器扩展中创建自定义RedirectToAction方法,如下所示:

public static MvcHtmlString ActionLinkWithQueryString(this HtmlHelper htmlHelper, 
    string linkText, string actionName)
{
    var routeValueDictionary = new RouteValueDictionary();

    return htmlHelper.ActionLinkWithQueryString(linkText, 
        actionName, routeValueDictionary);
}

public static MvcHtmlString ActionLinkWithQueryString(this HtmlHelper htmlHelper, 
   string linkText, string actionName, RouteValueDictionary routeValues)
{
    var queryString = HttpContext.Current.Request.QueryString;

    if (queryString.Count > 0)
    {
        foreach (string key in queryString.Keys)
        {
            routeValues.Add(key, queryString[key]);
        }
    }

    return htmlHelper.ActionLink(linkText, actionName, routeValues);
}
private RedirectToRouteResult RedirectToActionWithQueryString(string actionName)
{
    var queryString = Request.QueryString;
    var routeValues = new RouteValueDictionary();

    foreach (string key in queryString.Keys)
    {
        routeValues.Add(key, queryString[key]);
    }

    return RedirectToAction(actionName, routeValues);
}

您试图实现的目标与MVC的理念背道而驰。如果要在操作之间保留查询字符串数据,可以创建自定义ActionLink html帮助程序,如下所示:

public static MvcHtmlString ActionLinkWithQueryString(this HtmlHelper htmlHelper, 
    string linkText, string actionName)
{
    var routeValueDictionary = new RouteValueDictionary();

    return htmlHelper.ActionLinkWithQueryString(linkText, 
        actionName, routeValueDictionary);
}

public static MvcHtmlString ActionLinkWithQueryString(this HtmlHelper htmlHelper, 
   string linkText, string actionName, RouteValueDictionary routeValues)
{
    var queryString = HttpContext.Current.Request.QueryString;

    if (queryString.Count > 0)
    {
        foreach (string key in queryString.Keys)
        {
            routeValues.Add(key, queryString[key]);
        }
    }

    return htmlHelper.ActionLink(linkText, actionName, routeValues);
}
private RedirectToRouteResult RedirectToActionWithQueryString(string actionName)
{
    var queryString = Request.QueryString;
    var routeValues = new RouteValueDictionary();

    foreach (string key in queryString.Keys)
    {
        routeValues.Add(key, queryString[key]);
    }

    return RedirectToAction(actionName, routeValues);
}
您还可以在控制器或控制器扩展中创建自定义RedirectToAction方法,如下所示:

public static MvcHtmlString ActionLinkWithQueryString(this HtmlHelper htmlHelper, 
    string linkText, string actionName)
{
    var routeValueDictionary = new RouteValueDictionary();

    return htmlHelper.ActionLinkWithQueryString(linkText, 
        actionName, routeValueDictionary);
}

public static MvcHtmlString ActionLinkWithQueryString(this HtmlHelper htmlHelper, 
   string linkText, string actionName, RouteValueDictionary routeValues)
{
    var queryString = HttpContext.Current.Request.QueryString;

    if (queryString.Count > 0)
    {
        foreach (string key in queryString.Keys)
        {
            routeValues.Add(key, queryString[key]);
        }
    }

    return htmlHelper.ActionLink(linkText, actionName, routeValues);
}
private RedirectToRouteResult RedirectToActionWithQueryString(string actionName)
{
    var queryString = Request.QueryString;
    var routeValues = new RouteValueDictionary();

    foreach (string key in queryString.Keys)
    {
        routeValues.Add(key, queryString[key]);
    }

    return RedirectToAction(actionName, routeValues);
}