I upgraded my site to use ASP.Net MVC from traditional ASP.Net webforms. I'm using the MVC routing to redirect requests for old .aspx pages to their new Controller/Action equivalent:
我升级了我的站点以使用ASP。来自传统ASP的netmvc。净webforms。我正在使用MVC路由将旧.aspx页面的请求重定向到它们的新控制器/动作等效项:
routes.MapRoute(
"OldPage",
"oldpage.aspx",
new { controller = "NewController", action = "NewAction", id = "" }
);
This is working great for pages because they map directly to a controller and action. However, my problem is requests for images - I'm not sure how to redirect those incoming requests.
这对于页面非常有用,因为它们直接映射到控制器和操作。然而,我的问题是对图像的请求——我不确定如何重定向这些传入的请求。
I need to redirect incoming requests for http://www.domain.com/graphics/image.png to http://www.domain.com/content/images/image.png.
我需要将来自http://www.domain.com/graphics/image.png的请求重定向到http://www.domain.com/content/images/image.png。
What is the correct syntax when using the .MapRoute()
method?
使用.MapRoute()方法时,正确的语法是什么?
2 个解决方案
#1
48
You can't do this "out of the box" with the MVC framework. Remember that there is a difference between Routing and URL-rewriting. Routing is mapping every request to a resource, and the expected resource is a piece of code.
你不能用MVC框架“跳出框框”。请记住,路由和url重写之间是有区别的。路由是将每个请求映射到一个资源,而期望的资源是一段代码。
However - the flexibility of the MVC framework allows you to do this with no real problem. By default, when you call routes.MapRoute()
, it's handling the request with an instance of MvcRouteHandler()
. You can build a custom handler to handle your image urls.
然而,MVC框架的灵活性使您可以在不存在任何问题的情况下完成这项工作。默认情况下,当您调用routing . maproute()时,它使用MvcRouteHandler()实例处理请求。您可以构建一个自定义处理程序来处理您的图像url。
-
Create a class, maybe called ImageRouteHandler, that implements
IRouteHandler
.创建一个类,可能叫做ImageRouteHandler,它实现IRouteHandler。
-
Add the mapping to your app like this:
将映射添加到您的应用程序如下:
routes.Add("ImagesRoute", new Route("graphics/{filename}",
new ImageRouteHandler()));路线。添加(“ImagesRoute”、新路由(“graphics/{filename}”、新ImageRouteHandler()));
-
That's it.
就是这样。
Here's what your IRouteHandler
class looks like:
以下是你的IRouteHandler类:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Compilation;
using System.Web.Routing;
using System.Web.UI;
namespace MvcApplication1
{
public class ImageRouteHandler : IRouteHandler
{
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
string filename = requestContext.RouteData.Values["filename"] as string;
if (string.IsNullOrEmpty(filename))
{
// return a 404 HttpHandler here
}
else
{
requestContext.HttpContext.Response.Clear();
requestContext.HttpContext.Response.ContentType = GetContentType(requestContext.HttpContext.Request.Url.ToString());
// find physical path to image here.
string filepath = requestContext.HttpContext.Server.MapPath("~/test.jpg");
requestContext.HttpContext.Response.WriteFile(filepath);
requestContext.HttpContext.Response.End();
}
return null;
}
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;
}
return "";
}
}
}
#2
6
If you were to do this using ASP.NET 3.5 Sp1 WebForms you would have to create a seperate ImageHTTPHandler that implements IHttpHandler to handle the response. Essentially all you would have to do is put the code that is currently in the GetHttpHandler method into your ImageHttpHandler's ProcessRequest method. I also would move the GetContentType method into the ImageHTTPHandler class. Also add a variable to hold the name of the file.
如果你要使用ASP。您必须创建一个独立的ImageHTTPHandler,它实现IHttpHandler来处理响应。基本上,您所要做的就是将当前在GetHttpHandler方法中的代码放入ImageHttpHandler的ProcessRequest方法中。我还将GetContentType方法移动到ImageHTTPHandler类中。还可以添加一个变量来保存文件的名称。
Then your ImageRouteHanlder class looks like:
那么你的ImageRouteHanlder类看起来是:
public class ImageRouteHandler:IRouteHandler
{
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
string filename = requestContext.RouteData.Values["filename"] as string;
return new ImageHttpHandler(filename);
}
}
and you ImageHttpHandler class would look like:
ImageHttpHandler类看起来是这样的:
public class ImageHttpHandler:IHttpHandler
{
private string _fileName;
public ImageHttpHandler(string filename)
{
_fileName = filename;
}
#region IHttpHandler Members
public bool IsReusable
{
get { throw new NotImplementedException(); }
}
public void ProcessRequest(HttpContext context)
{
if (string.IsNullOrEmpty(_fileName))
{
context.Response.Clear();
context.Response.StatusCode = 404;
context.Response.End();
}
else
{
context.Response.Clear();
context.Response.ContentType = GetContentType(context.Request.Url.ToString());
// find physical path to image here.
string filepath = context.Server.MapPath("~/images/" + _fileName);
context.Response.WriteFile(filepath);
context.Response.End();
}
}
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;
}
return "";
}
#endregion
}
#1
48
You can't do this "out of the box" with the MVC framework. Remember that there is a difference between Routing and URL-rewriting. Routing is mapping every request to a resource, and the expected resource is a piece of code.
你不能用MVC框架“跳出框框”。请记住,路由和url重写之间是有区别的。路由是将每个请求映射到一个资源,而期望的资源是一段代码。
However - the flexibility of the MVC framework allows you to do this with no real problem. By default, when you call routes.MapRoute()
, it's handling the request with an instance of MvcRouteHandler()
. You can build a custom handler to handle your image urls.
然而,MVC框架的灵活性使您可以在不存在任何问题的情况下完成这项工作。默认情况下,当您调用routing . maproute()时,它使用MvcRouteHandler()实例处理请求。您可以构建一个自定义处理程序来处理您的图像url。
-
Create a class, maybe called ImageRouteHandler, that implements
IRouteHandler
.创建一个类,可能叫做ImageRouteHandler,它实现IRouteHandler。
-
Add the mapping to your app like this:
将映射添加到您的应用程序如下:
routes.Add("ImagesRoute", new Route("graphics/{filename}",
new ImageRouteHandler()));路线。添加(“ImagesRoute”、新路由(“graphics/{filename}”、新ImageRouteHandler()));
-
That's it.
就是这样。
Here's what your IRouteHandler
class looks like:
以下是你的IRouteHandler类:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Compilation;
using System.Web.Routing;
using System.Web.UI;
namespace MvcApplication1
{
public class ImageRouteHandler : IRouteHandler
{
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
string filename = requestContext.RouteData.Values["filename"] as string;
if (string.IsNullOrEmpty(filename))
{
// return a 404 HttpHandler here
}
else
{
requestContext.HttpContext.Response.Clear();
requestContext.HttpContext.Response.ContentType = GetContentType(requestContext.HttpContext.Request.Url.ToString());
// find physical path to image here.
string filepath = requestContext.HttpContext.Server.MapPath("~/test.jpg");
requestContext.HttpContext.Response.WriteFile(filepath);
requestContext.HttpContext.Response.End();
}
return null;
}
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;
}
return "";
}
}
}
#2
6
If you were to do this using ASP.NET 3.5 Sp1 WebForms you would have to create a seperate ImageHTTPHandler that implements IHttpHandler to handle the response. Essentially all you would have to do is put the code that is currently in the GetHttpHandler method into your ImageHttpHandler's ProcessRequest method. I also would move the GetContentType method into the ImageHTTPHandler class. Also add a variable to hold the name of the file.
如果你要使用ASP。您必须创建一个独立的ImageHTTPHandler,它实现IHttpHandler来处理响应。基本上,您所要做的就是将当前在GetHttpHandler方法中的代码放入ImageHttpHandler的ProcessRequest方法中。我还将GetContentType方法移动到ImageHTTPHandler类中。还可以添加一个变量来保存文件的名称。
Then your ImageRouteHanlder class looks like:
那么你的ImageRouteHanlder类看起来是:
public class ImageRouteHandler:IRouteHandler
{
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
string filename = requestContext.RouteData.Values["filename"] as string;
return new ImageHttpHandler(filename);
}
}
and you ImageHttpHandler class would look like:
ImageHttpHandler类看起来是这样的:
public class ImageHttpHandler:IHttpHandler
{
private string _fileName;
public ImageHttpHandler(string filename)
{
_fileName = filename;
}
#region IHttpHandler Members
public bool IsReusable
{
get { throw new NotImplementedException(); }
}
public void ProcessRequest(HttpContext context)
{
if (string.IsNullOrEmpty(_fileName))
{
context.Response.Clear();
context.Response.StatusCode = 404;
context.Response.End();
}
else
{
context.Response.Clear();
context.Response.ContentType = GetContentType(context.Request.Url.ToString());
// find physical path to image here.
string filepath = context.Server.MapPath("~/images/" + _fileName);
context.Response.WriteFile(filepath);
context.Response.End();
}
}
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;
}
return "";
}
#endregion
}