Hello
When i enter http://localhost:1984/Content/Images/iphone4g.jpg, image open. But when with controller Shop, not - http://localhost:1984/Shop/Content/Images/iphone4g.jpg.
I think, problem with path. How to add a new route that would have opened the image with http://localhost:1984/Shop/Content/Images/iphone4g.jpg.?
Loading

chirag pandya30 reputationPosted Mar 15, 2012, 10:16 PM EDT
1) ImageRouteHandler class
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
string filename = requestContext.RouteData.Values["filename"] as string;
if (string.IsNullOrEmpty(filename))
{
}
else
{
requestContext.HttpContext.Response.Clear();
requestContext.HttpContext.Response.ContentType = GetContentType(requestContext.HttpContext.Request.Url.ToString());
string filepath = HttpContext.Current.Server.MapPath("~/Content/images/" + filename);
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) Gloabal.asax.cs
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.Add("ImagesRoute", new Route("Home/Content/Images/{filename}", new ImageRouteHandler()));
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
3) Index.cshtml user Home View Folder
@{
ViewBag.Title = "Home Page";
}
@ViewBag.Message
chirag pandya30 reputationPosted Mar 14, 2012, 10:54 PM EDT
For that You can build a custom handler to handle your image urls. please follow following steps
1) Create a class, maybe called ImageRouteHandler, that implements
IRouteHandler.2) Add the mapping to your app like this:
routes.Add("ImagesRoute", new Route("Shop/{filename}", new ImageRouteHandler()));
Your ImageRouteHanlder class like below
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("~/iphone4g.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 "";
}
Mike JonsonRank #7,584 · 238 reputationPosted Mar 14, 2012, 11:55 PM EDT
becose i have object - Make make = new Make();
make.ImgItem - name of image
I did the following:
routes.MapRoute(
"ImagePath",
"{controller}/Content/Images/nameFile");
Add in View:
But not work
I want use your class, but how add Model.ImgItem in string filepath = requestContext.HttpContext.Server.MapPath("~/iphone4g.jpg");
chirag pandya30 reputationPosted Mar 15, 2012, 1:10 AM EDT
Just change following lines in GetHttpHandler function In ImageRouteHandler class which i have given to you eariler.
Then your problem will soveld.
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
string filename = requestContext.RouteData.Values["filename"] as string;
if (string.IsNullOrEmpty(filename))
{
}
else
{
requestContext.HttpContext.Response.Clear();
requestContext.HttpContext.Response.ContentType = GetContentType(requestContext.HttpContext.Request.Url.ToString());
string filepath = HttpContext.Current.Server.MapPath("~/Content/images/" + filename);
requestContext.HttpContext.Response.WriteFile(filepath);
requestContext.HttpContext.Response.End();
}
return null;
}
Mike JonsonRank #7,584 · 238 reputationPosted Mar 15, 2012, 2:56 AM EDT
chirag pandya30 reputationPosted Mar 15, 2012, 9:33 PM EDT
you have following line of code in your view.
just replace it with following line
because HTML.Routelink generate anchor link. so when it finally load it will look like src="Shop/Content/Images/iphone4g.jpg">ImagePath". so output will not genrate.
so please still any difficulty then let me know
Mike JonsonRank #7,584 · 238 reputationPosted Mar 15, 2012, 9:56 PM EDT
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
routes.Add("ImagesRoute", new Route("Content/Images/{filename}", new ImageRouteHandler()));
}
My View:
Show link: http://localhost:1984/ShopManager/Details/2?nameFile=iphone4g.jpg
And how correct to http://localhost:1984/Content/Images/iphone4g.jpg?
Mike JonsonRank #7,584 · 238 reputationPosted Mar 15, 2012, 11:13 PM EDT