software, productivity & more
Posts tagged ASP.Net MVC
Localization in ASP.Net MVC Projects
Aug 11th
Hello all,
As I am new to .Net Framework & ASP.Net MVC, I was looking in to various articles that would give clear step by step instructions as to how to add localization to ASP.Net MVC Projects. Especially, localization of string resources.
Here is a fantastic article on Localization in ASP.Net MVC projects.
Thanks to the author.
HTTP Test Client
Mar 3rd
When developing a RESTful Web service in ASP.Net MVC I was looking for a HTTP Test client that I can be used to test the RESTful API that I had created. It is easy to test HTTP GET with a browser. What I was looking for in the tool was the ability to HTTP POST/PUT/DELETE to any Url. Thanks to Stackoverflow where chris recommended a fantastic freeware tool called Fiddler
Definition from Fiddler’s website: Fiddler is a Web Debugging Proxy which logs all HTTP(S) traffic between your computer and the Internet. Fiddler allows you to inspect all HTTP(S) traffic, set breakpoints, and “fiddle” with incoming or outgoing data. Fiddler includes a powerful event-based scripting subsystem, and can be extended using any .NET language.
Thanks.
RESTful Web Service Using ASP.Net MVC
Feb 9th
Piers Lawson wrote excellent series of articles to guide you to write RESTful web service using ASP.Net MVC framework.
There are several parts (24 at the time of writing this post). But I think the first 12 parts are good for beginners to start with.
Part 1 – Part 2 – Part 3 – Part 4 – Part 5a – Part 5b – Part 6 – Part 7 – Part 8 – Part 9 – Part 10 – Part 11 – Part 12
Thanks.
REST for dummies
Feb 8th
In order to create a Web API for one of our products, I am learning about RESTful Web Services.
If you are just starting on REST, then here is the link that explains REST very clearly: How to Create a REST Protocol
To supplement this it is important to know the Common REST mistakes that developers make.
Thanks
Redirecting old urls to new in ASP.Net MVC
Feb 9th
Recently I migrated my classic ASP website to ASP.Net MVC. However, there were many websites still linking to specific .asp pages that no longer exist. I looked for some help on Google so that I could easily redirect the legacy URLs to the new one.
Thanks to Mikesdonetting post that gave me that ‘real code’. You will need to read this post for clear understanding.
While the above post covers the most important aspect of redirecting (LegacyUrlRoute class), I have modified the code so that it suits a more common scenario.
Here is the code:
namespace www.Helpers
{
class RedirectRule
{
public string OldUrlContains;
public string OldUrlContainsNot;
public string NewUrl;
public RedirectRule(string strOldUrlContains, string strOldUrlContainsNot, string strNewUrl)
{
OldUrlContains = strOldUrlContains;
OldUrlContainsNot = strOldUrlContainsNot;
NewUrl = strNewUrl;
}
};
public class LegacyUrlRoute : RouteBase
{
RedirectRule[] RedirectRules =
{
new RedirectRule("Notezilla/default.asp", "", "/Notezilla"),
new RedirectRule("RecentX/default.asp", "", "/RecentX"),
};
public override RouteData GetRouteData(HttpContextBase httpContext)
{
const string status = "301 Moved Permanently";
var request = httpContext.Request;
var response = httpContext.Response;
var legacyUrl = request.Url.ToString();
var newUrl = "";
foreach (RedirectRule rule in RedirectRules)
{
//if we don't have to check for a string that does not exist in the url
if (rule.OldUrlContainsNot.Length == 0)
{
//this does a case insensitive comparison
if (legacyUrl.IndexOf(rule.OldUrlContains, 0, StringComparison.CurrentCultureIgnoreCase) >= 0)
{
newUrl = rule.NewUrl;
}
}
else
{
//if we don't have to check for a string that does not exist in the url
if ((legacyUrl.IndexOf(rule.OldUrlContains, 0, StringComparison.CurrentCultureIgnoreCase) >= 0)
//so that it doesn't go in infinite loop since the end part of both urls are same
&& (!(legacyUrl.IndexOf(rule.OldUrlContainsNot, 0, StringComparison.CurrentCultureIgnoreCase) >= 0)))
{
newUrl = rule.NewUrl;
}
}
//found anything?
if (newUrl.Length > 0)
{
break;
}
}
if (newUrl.Length > 0)
{
response.Status = status;
response.RedirectLocation = newUrl;
response.End();
}
return null;
}
public override VirtualPathData GetVirtualPath(RequestContext requestContext,
RouteValueDictionary values)
{
return null;
}
}
}
Here is the explanation:
RedirectRule is a class that holds 1) the old url substring that we much check for, 2) the old url substring that should not exist in the url and 3) the new url which the user must be redirected to.
We initialize an array of such RedirectRule objects and check (inside the function GetRouteData) if the active url matches the criteria given in each rule. If it matches then we redirect to the new url.
Let us see, when we will have the case where RedirectRule.OldUrlContainsNot will need to be used.
Imagine that the old Url was ‘Notezilla/NotezillaSetup.exe’ and the new Url is ‘/Downloads/Notezilla/NotezillaSetup.exe’. In our case if we just check for the old Url then we will end up in infinite loop since the end part of both old and new Url is same (Notezilla/NotezillaSetup.exe). For this reason we need OldUrlContainsNot. In this case we will set OldUrlContainsNot to ‘/Downloads’ so that we do not get in to infinite loop.
Once you are done with the above code just add LegacyUrlRoute to existing routes in Global.asax.cs
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.Add(new LegacyUrlRoute());
}
That’s it.
Thanks.
Redirecting .asp to aspx in ASP.Net MVC
Dec 27th
I am learning ASP.Net MVC and I am loving it.
In the process of migrating our existing classic asp website to ASP.Net MVC we realized that we would still need an .asp file as part of the web app. It’s the redirect.asp file that all our products call to go to different parts of our website.
Fortunately, ASP.Net MVC provides a neat way to do this without actually creating an .asp file. You can route the .asp call to .aspx (MVC View) and have all your code in ASP.Net MVC controller.
Go to Global.asax.cs and write the following code:
routes.MapRoute("Redirect", "redirect.asp", new { controller = "Home", action = "Redirect" });
That’s it.
Note that the redirect.asp file should not (and does not) exist. The routing engine would just route the call to an action that you specify.
Thanks.
Returning String as ActionResult in ASP.Net MVC
Oct 27th
I am learning ASP.Net MVC now and loving it. If you to read more praises about ASP.Net MVC search for MVC on StackOverflow.
Ok. Not every time you may want to return a View for an action. Sometimes, you must just return some XML or string.
To do this just use the this.Content function to return ActionResult.
return this.Content("Your string here")
Thanks.
ASP.Net MVC for Beginners/Dummies
Sep 5th
If you are just starting with ASP.Net MVC (Model View Controller) framework then here are the must read articles to learn the basics. These articles will get you clear picture about the framework.
ASP.NET MVC Framework (Part 1)
ASP.NET MVC Framework (Part 2) – URL Routing
ASP.NET MVC Framework (Part 3) – Passing ViewData from Controllers to Views
ASP.NET MVC Framework (Part 4) – Handling Form Edit and Post Scenarios
Thanks.