DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Securing Your Software Supply Chain with JFrog and Azure
Register Today

Trending

  • Replacing Apache Hive, Elasticsearch, and PostgreSQL With Apache Doris
  • Essential Architecture Framework: In the World of Overengineering, Being Essential Is the Answer
  • Web Development Checklist
  • Top 10 Pillars of Zero Trust Networks

Trending

  • Replacing Apache Hive, Elasticsearch, and PostgreSQL With Apache Doris
  • Essential Architecture Framework: In the World of Overengineering, Being Essential Is the Answer
  • Web Development Checklist
  • Top 10 Pillars of Zero Trust Networks
  1. DZone
  2. Coding
  3. Frameworks
  4. Goodbye Http Handler, Hello FileResult

Goodbye Http Handler, Hello FileResult

Michael Ceranski user avatar by
Michael Ceranski
·
Mar. 12, 10 · News
Like (0)
Save
Tweet
Share
11.26K Views

Join the DZone community and get the full member experience.

Join For Free

If you have been developing applications in ASP.NET MVC then you are probably familiar with the ActionResult class. The ActionResult is the most common type of object returned from an action. When building MVC apps, most of time you will use the ActionResult class.

Last week while I was working on my open source project WeBlog, I built an HTTP Handler to serve up images. I started using an HTTP Handler for images because I needed a mechanism to prevent bandwidth leeching. The only bad thing about using an HTTP handler for images is that you end up with some pretty ugly URLS. In my case the URL ended up looking like this:

/Image.axd?image=sample.png

Luckily, my friend Ron noticed my new HTTP Handler and mentioned that I could have accomplished the same thing with a controller action that returned a FileResult instead. After a bit of investigation, I realized that Ron was absolutely right. I deleted my HTTP Handler and replaced it with this code, which was added to the Home Controller:

private string  GetContentType(string filename)
{
FileInfo file = new FileInfo(filename);

switch (file.Extension.ToUpper())
{
//images
case ".PNG" : return "image/png";
case ".JPG" : return "image/jpeg";
case ".JPEG": return "image/jpeg";
case ".GIF" : return "image/gif";
case ".BMP" : return "image/bmp";
case ".TIFF": return "image/tiff";
default:
throw new NotSupportedException("The Specified File Type Is Not Supported");
}
}

public FileResult GetImage(string id)
{
string path = Path.Combine(Engine.GetImageDirectory().FullName, id);
return base.File(path, GetContentType( path ) );
}

Since this code resides in my Home controller I would need to use the URL like “/Home/GetImage/sample.png” to display an image. Admittedly this URL is still a big ugly, so I decided to use a custom route to clean it up. The new custom route is named “Images” and is mapped it to the Home controller’s GetImage method. Here is the entry used in the global.asax file:

routes.MapRoute("Images",
"Images/{id}",
new { controller = "Home", action = "GetImage", id = "" });

Now I can display images by using the following URL:

”/Images/sample.png”

To the end user, this looks like a traditional file path. However, in reality there is no “Images” folder in the root directory. “Images” is just the name of the route being used. In reality, the image files are actually stored in the App_Data/Images folder.

By using a FileResult object with MVC you not only get a pretty URL but you also get a lot of flexibility on where you want your images to reside. You can store images anywhere you want and the URL will never need to change!

 

Open source ASP.NET MVC Object (computer science) app application Bandwidth (computing) ASP.NET Directory

Published at DZone with permission of Michael Ceranski, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Replacing Apache Hive, Elasticsearch, and PostgreSQL With Apache Doris
  • Essential Architecture Framework: In the World of Overengineering, Being Essential Is the Answer
  • Web Development Checklist
  • Top 10 Pillars of Zero Trust Networks

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: