- How-To: Build Language dropdown with ASP.NET MVC and LINQ
-
Posted by oVan on Wednesday, May 28, 2008 | PermaLink | 0 comments
Here's a quick way to build a language dropdown box with ASP.NET MVC Preview 3, without using a database table. In total we need only 3 lines of code.
1. Controller:
In our controller, we add the following code to the controller action:
List<System.Globalization.CultureInfo> Locales =
System.Globalization.CultureInfo.GetCultures(
System.Globalization.CultureTypes.NeutralCultures).ToList();ViewData["Locales"] = new SelectList(
Locales
.OrderBy(lc => lc.Name.Length)
.ThenBy(lc => lc.DisplayName),
"Name",
"DisplayName");First we create a list of CultureInfo and store it in a variable Locales. For this example I've used NeutralCultures to limit the entries to the most used languages, otherwise you'll get a very long list.
Next we create a ViewData object and fill it using SelectList, passing along our Locales variable.
Note: I've sorted Locales twice, first with OrderBy and then with ThenBy. As parameter we throw in a Lambda expression to transform our Locales and get the member we want. The reason I sort on Name.Length first is that the Invariant CultureInfo has an empty Name and thus it will be sorted to the top of the list.
After that we simply render our View (in this case passing along a Contact record from the database.
2. View:
The code in our view is even simpler. With the help of Extensions and MVC HtmlHelper objects we have our dropdown box in no time:
The only line of code to render the fully functional html select dropdown box is this:
<%=Html.DropDownList("Locales") %>
Here is the final result:
A quick look at the produced code shows us the clean XHTML underneath:
<select name="Locales" id="Locales">
<option value="">Invariant Language (Invariant Country)</option>
<option value="af">Afrikaans</option>
<option value="sq">Albanian</option>
<option value="ar">Arabic</option>
<option value="hy">Armenian</option>
<option value="az">Azeri</option>
<option value="eu">Basque</option>
<option value="be">Belarusian</option>
<option value="bg">Bulgarian</option>
...Ps: in the screenshots above I've used a test version of TheSansMono OpenType by Luc(as) de Groot. This font will be published soon on the LucasFonts website, together with TheSansCondensed OpenType. Typographer Luc(as) de Groot is also known for the Calibri and Consolas fonts in Windows Vista and Microsoft Office 2007.
- FIX: GoogleBot produces "Cannot use a leading .. to exit above the top directory" in ASP.NET
-
Posted by oVan on Sunday, April 06, 2008 | PermaLink | 0 comments
I found numerous "Cannot use a leading .. to exit above the top directory" errors in my Elmah-logs lately, all of them generated by the ASP.NET engine after a visit from GoogleBot. It is caused by using URL rewriting in your aspx-pages in order to have more readable URL's and better search engine rankings.
Funnily, this only started after I submitted dynamically generated sitemaps to the Google Webmaster Tools for my clients websites. Instead of improved ranking and indexing, however it resulted in exclusion of all failing URL's.
For more background information about this error, which is generated only for certain user agents including GoogleBot and Yahoo! Slurp, you can do a simple Google search. There are a few solutions to fix this, but most involve using a custom base page class or creating different .browser files for each problematic user-agent in the App_Browser directory. Having done custom browser.ini solutions with Browserhawk years ago, I did not feel like starting all over again with googlebot.browser, yahooslurp.browser etc.
By far the easiest solution is a simple change in your web.config:
Find the <authentication> section, then change the <forms> line by adding:cookieless="UseCookies"
Voila, the problem is fixed. Note that this disables cookieless functionality for forms, so if you really need that you should use a different solution.
Labels: .NET, asp.net, browser, bug, Google, Microsoft, problem, Visual Studio 2005, Visual Studion 2008, vs2005, vs2008
- How-to: Viewing current Route in ASP.NET MVC
-
Posted by oVan on Friday, December 21, 2007 | PermaLink | 1 comments
For those who are playing with the ASP.NET MVC CTP bits, here's an easy way to check the routing info that was used to arrive at your View. You need to enter this into your Site.Master shared view, put it just below the menu-div.
<div class="debug">
<dl>
<dt>Current routing url:</dt>
<dd><%= ViewContext.RouteData.Route.Url.ToString()%>
</dd>
<dt> Current routing values:</dt>
<dd>
<ol><%= ViewContext.RouteData.Values.ToFormattedList("<li>{0}</li>") %></ol>
</dd>
</dl>
</div>
- Visual Studio 2008 and .NET 3.5 are here!
-
Posted by oVan on Monday, November 19, 2007 | PermaLink | 0 comments
The Wait for Visual Studio 2008 is Over!
On Monday, Nov. 19, Microsoft announced that Visual Studio 2008 and the .NET Framework 3.5 were released to manufacturing (RTM). With more than 250 new features,Visual Studio 2008 includes significant enhancements in every edition, including Visual Studio Express and Visual Studio Team System. Developers of all levels – from hobbyists to enterprise development teams – now have a consistent, secure and reliable solution for developing applications for the latest platforms: the Web, Windows Vista, Windows Server 2008, the 2007 Office system, and beyond.
- MSDN Subscribers: Get Visual Studio 2008 Now
- Download Trial Editions of Visual Studio 2008
- Download Visual Studio 2008 Express Editions
- Download the .NET Framework 3.5
Labels: .NET, asp.net, C#, free, Microsoft, MSDN, Visual Studio Orcas, web develoment, Windows
- ResolveUrl with no Page or Control in sight
-
Posted by oVan on Monday, October 29, 2007 | PermaLink | 1 commentsWhile working (sweating) on a client's website, I needed a reliable ResolveUrl method without making use of the Page or Control classes. A few quick Googles resulted in some working functions, although not all of them resulted in the same output as the real ResolveUrl.
Some solutions use the VirtualPathUtility for that, however it throws an HttpException when the url to be resolved contains a query string. The most elegant, and from what I can tell also the most correct function came from a comment by Richard Deeming on Rick Strahl's blog:
public static string ResolveUrl(string originalUrl)
{
if (!string.IsNullOrEmpty(originalUrl) && '~' == originalUrl[0])
{
int index = originalUrl.IndexOf('?');
string queryString = (-1 == index) ? null : originalUrl.Substring(index);
if (-1 != index) originalUrl = originalUrl.Substring(0, index);
originalUrl = VirtualPathUtility.ToAbsolute(originalUrl) + queryString;
}
return originalUrl;
}
To resolve the full URL, he uses the Uri and UriBuilder classes:
public static string ResolveServerUrl(string serverUrl, bool forceHttps)
{
Uri result = HttpContext.Current.Request.Url;
if (!string.IsNullOrEmpty(serverUrl))
{
serverUrl = ResolveUrl(serverUrl);
result = new Uri(result, serverUrl);
}
if (forceHttps && !string.Equals(result, Uri.UriSchemeHttps))
{
UriBuilder builder = new UriBuilder(result);
builder.Scheme = Uri.UriSchemeHttps;
builder.Port = 443;
result = builder.Uri;
}
return result.ToString();
}
This code works beautifully!
Source: read the original article and many interesting comments: ResolveUrl-without-page.
- Adding a Content Role in MWPSK
-
Posted by oVan on Thursday, March 22, 2007 | PermaLink | 0 commentsHere's an easy solution to add a Content Editor role in the MyWebPagesStarterKit without defining a new role (which would take a lot of work):
Currently the authenticated user that is not part of the administrator role can do nothing more than an anonymous (unauthenticated user) user on the website.
So let's assume that those non-admin authenticated users are just content editors. We want them to be able to add/change the content of the defined section controls, without the ability to delete sections or to change the sitemap structure.
Here are the two easy steps to accomplish this:
1) Change the following in ~/Default.aspx.cs:
foreach (ISection section in _page.Sections)
{
SectionControlBaseClass ctl = (SectionControlBaseClass)LoadControl(section.UserControl);
if (User.Identity.IsAuthenticated && User.IsInRole(RoleNames.Administrators.ToString()))
Into:
foreach (ISection section in _page.Sections)
{
SectionControlBaseClass ctl = (SectionControlBaseClass)LoadControl(section.UserControl);
if ((User.Identity.IsAuthenticated))
The above change will enable the admin functions for sections on each page, whithout enabling the administration menu for administrators.
2) Change the following line in ~/SectionControls/SectionAdmin.ascx:<asp:Button runat="server" ID="btnDeleteSection" OnClick="btnDeleteSection_Click" Text="<%$ Resources:stringsRes, glb__DeleteSection%>" CausesValidation="false" UseSubmitBehavior="false" />
Into:
<% if (Context.User.IsInRole(MyWebPagesStarterKit.RoleNames.Administrators.ToString())) {%><asp:Button runat="server" ID="btnDeleteSection" OnClick="btnDeleteSection_Click" Text="<%$ Resources:stringsRes, glb__DeleteSection%>" CausesValidation="false" UseSubmitBehavior="false" /><% } %>
The above change will remove the ability to delete section controls.
The result will enable authenticated users to update the content of the website, withouth the ability to destroy it :-)
Hope this helps!Labels: Administrator, asp.net, CodePlex, content, editor, fix, Microsoft, MWPSK, MyWebPagesStarterKit, patch, role, starter kit
