UrlRoutingModule中的代码崩溃了

时间:2014-06-11 21:48:13

标签: ithit-webdav-server

调试我的代码时,UrlRoutingModule类中的代码多次崩溃。有两种错误:

  • 空参考例外:

foreach (var route in l) { RouteTable.Routes.Add(route); <-- It crashed here because route is NULL }

我添加了#34; if(route!= null)&#34;在上述声明之前它似乎解决了问题。

  • ArgumentException:提供的路由已存在于路径集合中。该集合可能不包含重复的路由。

第二个例外也发生在上面的同一行。

我该怎么做才能解决这个问题,因为一旦发生这个错误,我就不得不关闭IIS Express和Visual Studio,因为整个网站都被软化了。

1 个答案:

答案 0 :(得分:0)

这是UrlRoutingModule类中的错误。它已在我们今天发布的最新版本中修复。以下是此类的正确实现:

/// <summary>
/// Forbids MVC routing of WebDAV requests that should be processed by WebDAV handler.
/// </summary>
/// <remarks>
/// This module is needed for ASP.NET MVC application to forbid routing
/// of WebDAV requests that should be processed by <see cref="DavHandler"/>.
/// It reads DavLocations section and inserts ignore rules for urls that correspond
/// to these locations.
/// </remarks>
public class UrlRoutingModule : IHttpModule
{
    /// <summary>
    /// Represents route to be ignored. 
    /// </summary>
    /// <remarks>
    /// Required to insert ignore route at the beginning of routing table. We can not 
    /// use the <see cref="RouteCollection.IgnoreRoute"/> as it adds to the and of the 
    /// routing table.
    /// </remarks>
    private sealed class IgnoreRoute : Route
    {
        /// <summary>
        /// Creates ignore route.
        /// </summary>
        /// <param name="url">Route to be ignored.</param>
        public IgnoreRoute(string url)
            : base(url, new StopRoutingHandler())
        {
        }
        public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary routeValues)
        {
            return null;
        }
    }

    /// <summary>
    /// Indicates that the web application is started.
    /// </summary>
    private static bool appStarted = false;

    /// <summary>
    /// Application start lock.
    /// </summary>
    private static object appStartLock = new Object();

    void IHttpModule.Init(HttpApplication application)
    {
        // Here we update ASP.NET MVC routing table to avoid processing of WebDAV requests.
        // This should be done only one time during web application start event.

        lock (appStartLock)
        {
            if (appStarted)
            {
                return;
            }
            appStarted = true;
        }

        DavLocationsSection davLocationsSection = DavLocationsSection.Instance;

        int routeInsertPosition = 0;
        foreach (DavLocation location in davLocationsSection.Locations)
        {
            string[] methods = location.Verbs.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries)
                .Select(verb => verb.Trim().ToUpper()).ToArray();

            string prefix = location.Path.Trim(new[] { '/', '\\' });
            string path = (prefix == string.Empty ? string.Empty : prefix + "/") + "{*rest}";

            IgnoreRoute ignoreRoute = new IgnoreRoute(path);
            if (methods.Length > 0)
            {
                ignoreRoute.Constraints = new RouteValueDictionary { { "httpMethod", new HttpMethodConstraint(methods) } };
            }
            RouteTable.Routes.Insert(routeInsertPosition++, ignoreRoute);
        }
    }

    public void Dispose()
    {
    }
}