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 pandyaPosted Mar 16, 2012, 2:16 AM
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
Mike JonsonPosted Mar 16, 2012, 3:13 AM
Mike JonsonPosted Mar 16, 2012, 1:56 AM
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?
chirag pandyaPosted Mar 16, 2012, 1:33 AM
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 JonsonPosted Mar 15, 2012, 6:56 AM
chirag pandyaPosted Mar 15, 2012, 5:10 AM
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 JonsonPosted Mar 15, 2012, 3:55 AM
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 pandyaPosted Mar 15, 2012, 2:54 AM
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 "";
}