错误判定定义及System.Web.Routing.RouteValueDictionary 没有扩展方法

发布于 2024-10-14 16:47:37 字数 2105 浏览 3 评论 0原文

我正在 4GuysFromRolla 网站上阅读 Scott Mitchell 编写的有关在 ASP.NET MVC 2 中对数据网格进行排序和分页的教程。我收到错误 CS1061:“System.Web.Routing.RouteValueDictionary”不包含“AddQueryStringParameters”的定义,并且找不到接受“System.Web.Routing.RouteValueDictionary”类型的第一个参数的扩展方法“AddQueryStringParameters” (您是否缺少 using 指令或程序集引用?)。我不确定是否需要添加 dll 引用或其他内容。请有人建议如何解决这个问题,提前致谢。我也下载了demo,没有问题。错误位于 PagerLink.ascx 文件中..routeData.AddQueryStringParameters(); // 错误指向此处

RouteValueDictionaryExtensions.cs 看起来像这样(这是帮助程序文件)...

using System.Web.Routing;
namespace Web
{
  public static class RouteValueDictionaryExtensions
  {
     public static RouteValueDictionary  
     AddQueryStringParameters(this RouteValueDictionary dict)
     {
       var querystring = HttpContext.Current.Request.QueryString;

        foreach (var key in querystring.AllKeys)
            if (!dict.ContainsKey(key))
                dict.Add(key, querystring.GetValues(key)[0]);

        return dict;
    }

    public static RouteValueDictionary ExceptFor(this RouteValueDictionary  
                dict, params string[] keysToRemove)
    {
        foreach (var key in keysToRemove)
            if (dict.ContainsKey(key))
                dict.Remove(key);

        return dict;
    }
}

}

Global.asax.cs 看起来像这样...

enter code here
namespace GridDemosMVC
{
// Note: For instructions on enabling IIS6 or IIS7 classic mode, 
// visit http://go.microsoft.com/?LinkId=9394801

public class MvcApplication : System.Web.HttpApplication
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = 
            UrlParameter.Optional } // Parameter defaults
   );

  }

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        RegisterRoutes(RouteTable.Routes);
    }
}
}

我还使用 Dynamic.cs 文件,该文件可以在 microsoft 上下载。

I am going through tutorial at 4GuysFromRolla website regarding Sorting and Paging a Grid of Data in ASP.NET MVC 2 by Scott Mitchell. I am receiving an error CS1061: 'System.Web.Routing.RouteValueDictionary' does not contain a definition for 'AddQueryStringParameters' and no extension method 'AddQueryStringParameters' accepting a first argument of type 'System.Web.Routing.RouteValueDictionary' could be found (are you missing a using directive or an assembly reference?). I am not sure if I need to add a dll reference or something else. Please could someone advise how to solve this thanks in advance. Also I downloaded the demo and there is no problem. error is in PagerLink.ascx file..routeData.AddQueryStringParameters(); // error pointing here

RouteValueDictionaryExtensions.cs looks like this (this is the helper file)...

using System.Web.Routing;
namespace Web
{
  public static class RouteValueDictionaryExtensions
  {
     public static RouteValueDictionary  
     AddQueryStringParameters(this RouteValueDictionary dict)
     {
       var querystring = HttpContext.Current.Request.QueryString;

        foreach (var key in querystring.AllKeys)
            if (!dict.ContainsKey(key))
                dict.Add(key, querystring.GetValues(key)[0]);

        return dict;
    }

    public static RouteValueDictionary ExceptFor(this RouteValueDictionary  
                dict, params string[] keysToRemove)
    {
        foreach (var key in keysToRemove)
            if (dict.ContainsKey(key))
                dict.Remove(key);

        return dict;
    }
}

}

Global.asax.cs looks like this...

enter code here
namespace GridDemosMVC
{
// Note: For instructions on enabling IIS6 or IIS7 classic mode, 
// visit http://go.microsoft.com/?LinkId=9394801

public class MvcApplication : System.Web.HttpApplication
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = 
            UrlParameter.Optional } // Parameter defaults
   );

  }

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        RegisterRoutes(RouteTable.Routes);
    }
}
}

I am also using Dynamic.cs file which is available at microsoft to download.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱冒险 2024-10-21 16:47:37

您需要使用扩展方法为命名空间添加 using 语句和 <%@ Import 指令。

或者,您可以将扩展方法移至项目的命名空间中。

You need to add a using statement and <%@ Import directive for the namespace with the extension method.

Alternatively, you can move the extension method into your project's namespace.

旧伤还要旧人安 2024-10-21 16:47:37

添加到PagerLink.ascx文件中
<%@ Import Namespace="你的项目命名空间" %>

add in PagerLink.ascx file
<%@ Import Namespace="your project name space" %>

深爱成瘾 2024-10-21 16:47:37

在 2 个用户控件 (PagerLink.ascxSmartLink.ascx) 中引用命名空间 web,如下所示。

<%@ Import Namespace="Web"%>

如果您更改了现有命名空间,请使用项目的适当命名空间。

Refer the namespace web in 2 user controls (PagerLink.ascx & SmartLink.ascx) as shown below.

<%@ Import Namespace="Web"%>

If you have changed the existing namespace, use the appropriate namespace of your project.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文