这是我的RegisterRoutes:
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 /指数。 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()));
但是,当我为了让子文件夹将其更改为
routes.Add("ImagesRoute", new Route("graphics/{*filename}", new HttpRuntime.Routing.ImageRouteHandler()));
,然后重定向每个URL动作/图形。
例如当我在主页/ Index.cshtml有
$(function() {
$("#divJsTreeDemo").jstree({
"json_data" : {
"ajax" : {
"url": "@Url.Action("GetNodesJson", "Home")"
//"url": "@Url.Action("GetTreeData", "Home")"
,"async" : true
为生成的HTML页面上的AJAX调用的URL变得
"url": "/graphics?action=GetNodesJson&controller=Home"
这是为什么? 我该如何解决这个问题? 如果我将我的ImagesRoute移动到底部,JavaScript正确路由,但是我不能获得更多的远程图像,因为它们被路由到控制器“图形”,它不存在 - > Exception no such view ...
我绝对喜欢aspnetmvc ...我绝对讨厌路由。我感到你的痛苦。对不起,我无法帮忙。 – Jamiec
哦,有一个可能的帮助是我在使用来自http://mvccontrib.codeplex.com/的路由调试器时遇到路由问题。 – Jamiec