Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.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
C# MVC URL路由,路由到错误的路由,为什么?_C#_Asp.net Mvc_Asp.net Mvc 3_Model View Controller_Url Routing - Fatal编程技术网

C# MVC URL路由,路由到错误的路由,为什么?

C# MVC URL路由,路由到错误的路由,为什么?,c#,asp.net-mvc,asp.net-mvc-3,model-view-controller,url-routing,C#,Asp.net Mvc,Asp.net Mvc 3,Model View Controller,Url Routing,问题: 这是我的注册表: public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.Add("ImagesRoute", new Route("graphics/{*filename}", new HttpRuntime.Routing.ImageRout

问题:

这是我的注册表:

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");


            routes.Add("ImagesRoute", new Route("graphics/{*filename}", new HttpRuntime.Routing.ImageRouteHandler()));


            // insert_dateti
            routes.MapRoute(
                "Default", // Routenname
                "{controller}/{action}/{id}", // URL mit Parametern
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameterstandardwerte
                //new { controller = "Home", action = "Splash", id = UrlParameter.Optional } // Parameterstandardwerte
                //new { controller = "Folders", action = "Content", id = UrlParameter.Optional } // Parameterstandardwerte
            );



        } // End Sub RegisterRoutes
这是我的路线管理员

using System;
using System.IO;
using System.Web;
using System.Linq; 
using System.Web.UI;
using System.Web.Routing;
using System.Web.Compilation; 
using System.Collections.Generic; 


namespace HttpRuntime.Routing
{ 


    public class ImageRouteHandler : IRouteHandler 
    {


        public string MapRemoteLocation(string strPath)
        {
            if (string.IsNullOrEmpty(strPath))
                return "";

            string strBaseURL = "http://madskristensen.net/themes/standard/";
            return strBaseURL + strPath;
        }

        public IHttpHandler GetHttpHandler(RequestContext requestContext) 
        { 


            string filename = requestContext.RouteData.Values["filename"] as string; 

            if (string.IsNullOrEmpty(filename)) 
            {
                // return a 404 HttpHandler here
                requestContext.HttpContext.Response.StatusCode = 404;
                requestContext.HttpContext.Response.End();
                return null;
            } // End if (string.IsNullOrEmpty(filename)) 
            else 
            { 
                requestContext.HttpContext.Response.Clear();
                requestContext.HttpContext.Response.ContentType = GetContentType(filename);
                requestContext.HttpContext.Response.Redirect(MapRemoteLocation(filename));


                // find physical path to image here.   
                //string filepath = requestContext.HttpContext.Server.MapPath("~/test.jpg");


                // string stPath = requestContext.HttpContext.Request.Url.AbsolutePath;
                //requestContext.HttpContext.Response.WriteFile(filepath); 
                //requestContext.HttpContext.Response.End();
            } // End Else of if (string.IsNullOrEmpty(filename)) 

            return null; 
        } // End Function GetHttpHandler


        public static void MsgBox(object obj)
        {
            if (obj != null)
                System.Windows.Forms.MessageBox.Show(obj.ToString());
            else
                System.Windows.Forms.MessageBox.Show("obj is NULL");
        } // End Sub MsgBox


        private static string GetContentType(String path) 
        { 
            switch (Path.GetExtension(path)) 
            { 
                case ".bmp": return "Image/bmp"; 
                case ".gif": return "Image/gif"; 
                case ".jpg": return "Image/jpeg"; 
                case ".png": return "Image/png"; 
                default: break;
            } // End switch (Path.GetExtension(path))  
            return "";
        } // End Function GetContentType


    } // End Class ImageRouteHandler : IRouteHandler 


}  // End Namespace HttpRuntime.Routing
这样做的目的是,当我在/Home/Index.cshtml中有这个

<img src="graphics/mads2011.png?v=123" title="Compose E-Mail" alt="Compose E-Mail" />
但是当我把它改成

routes.Add("ImagesRoute", new Route("graphics/{*filename}", new HttpRuntime.Routing.ImageRouteHandler()));
为了允许子文件夹,它会将每个url操作重定向到/graphics

e、 当我有

$(function () {
        $("#divJsTreeDemo").jstree({ 
        "json_data" : {
            "ajax" : {
                 "url": "@Url.Action("GetNodesJson", "Home")"
                 //"url": "@Url.Action("GetTreeData", "Home")"
                ,"async" : true
在Home/Index.cshtml中

<img src="graphics/mads2011.png?v=123" title="Compose E-Mail" alt="Compose E-Mail" />
结果HTML页面上AJAX调用的URL变为

"url": "/graphics?action=GetNodesJson&amp;controller=Home"
为什么会这样? 我怎样才能解决这个问题?
如果我将ImagesRoute移动到底部,JavaScript将正确路由,但是我无法获得更多的远程图像,因为它们被路由到控制器“graphics”,而控制器“graphics”不存在-->异常没有这样的视图…

当从路由构建链接时,使用
{*filename}
ImagesRoute将满足所有条件,因此,如果该路由位于routes表中的
Default
路由之前,则该路由将始终用于构建链接。但是,在处理请求时,它会按预期工作,因为只有启动
http://mysite.com/graphics/...

为了解决此问题,您需要将ImagesRoutes移动到默认路由下方,然后需要向默认路由添加一个
RouteConstraint
,以便以
graphics
开头的任何请求都会使默认路由失败

按如下所示更改默认路线:

routes.MapRoute(
    "Default", // Routenname
    "{controller}/{action}/{id}", // URL mit Parametern
    new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // defaults
    new { controller = @"^(?!graphics).+" } // constraint
);

我绝对喜欢aspnetmvc。。。我绝对讨厌路由。我感觉到你的痛苦。很抱歉,我帮不上忙。哦,一个可能的帮助是,我在遇到路由问题时使用路由调试器。太好了,很有效!非常感谢你!尽管我必须说,我仍然不明白为什么@Url.Action(“GetNodesJson”,“Home”)(即/Home/GetNodesJson/graphics/which)或者为什么@Url.Action(“ActionName”,“ControllerName”)变成graphics/controllername/ActionName。不知怎的,我猜,它只是将扩展的route2填充到route1…我想我必须阅读MVC的源代码才能理解路由部分。因为你的ImagesRoute URL参数由一个贪婪的匹配组成,它匹配MVC构建的任何链接。在链接构建方面,一切ing符合该路线。请标记为已回答。谢谢。