คำถาม:
นี่คือ 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/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()));
แต่เมื่อฉันเปลี่ยนมันเป็น
routes.Add("ImagesRoute", new Route("graphics/{*filename}", new HttpRuntime.Routing.ImageRouteHandler()));
เพื่ออนุญาตโฟลเดอร์ย่อย จากนั้นระบบจะเปลี่ยนเส้นทางทุกการกระทำของ URL ไปที่ /graphics
เช่น. เมื่อฉันมี
$(function () {
$("#divJsTreeDemo").jstree({
"json_data" : {
"ajax" : {
"url": "@Url.Action("GetNodesJson", "Home")"
//"url": "@Url.Action("GetTreeData", "Home")"
,"async" : true
ใน Home/Index.cshtml
URL สำหรับการเรียก AJAX บนหน้า HTML ที่เป็นผลลัพธ์จะกลายเป็น
"url": "/graphics?action=GetNodesJson&controller=Home"
ทำไมเป็นเช่นนี้? และฉันจะแก้ไขสิ่งนี้ได้อย่างไร? ถ้าฉันย้าย ImagesRoute ของฉันไปที่ด้านล่าง JavaScript จะกำหนดเส้นทางอย่างถูกต้อง แต่จากนั้นฉันก็ไม่สามารถรับรูปภาพระยะไกลได้อีกต่อไป เนื่องจากมีการกำหนดเส้นทางไปยังตัวควบคุม "กราฟิก" ซึ่งไม่มีอยู่ --> ข้อยกเว้นไม่มีมุมมองดังกล่าว...