這是我的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