ASP.NET MVC Razor View Engine and Resources

The Razor view engine shipped with ASP.NET MVC 3 is very clean and has some nice coding enhancements, see a introduction on Scott Gu’s blog.  As I was converting a application over to use the new view engine, I was stumped to find that the global/local resource method was not directly accessible as it was with the default view engine.  In order to access a resource you would have to type the following expression ViewContext.HttpContext.GetGlobalResourceObject(classKey, resourceKey) which is quite length.  To mitigate this, I create some common extensions to clean up the code.  Below shows a clean way of doing this.

public static class CommonHtmlExtensions
{
public static object GetGlobalResource(this HtmlHelper htmlHelper, string classKey, string resourceKey)
{
return htmlHelper.ViewContext.HttpContext.GetGlobalResourceObject(classKey, resourceKey);
}

public static object GetGlobalResource(this HtmlHelper htmlHelper, string classKey, string resourceKey, CultureInfo culture)
{
return htmlHelper.ViewContext.HttpContext.GetGlobalResourceObject(classKey, resourceKey, culture);
}

public static object GetLocalResource(this HtmlHelper htmlHelper, string classKey, string resourceKey)
{
return htmlHelper.ViewContext.HttpContext.GetLocalResourceObject(classKey, resourceKey);
}

public static object GetLocalResource(this HtmlHelper htmlHelper, string classKey, string resourceKey, CultureInfo culture)
{
return htmlHelper.ViewContext.HttpContext.GetLocalResourceObject(classKey, resourceKey, culture);
}

}
 
In the view you will have consolidate resource calls.  Hopefully they will add something like this in the future of ASP.NET MVC.
 
@Html.GetGlobalResource("CommonText", "Some_ResourceKey")

TFS 2010 Build and Deploy with Web Deploy

It hasn’t been until recently that I’ve messed around with some of the web application deployment tools Visual Studio and TFS have to offer.  So I started with the one click publish which you can find in Visual Studio when you have your web application highlighted.  It’s really cool that you can deploy to a web server directly from Visual Studio, but what’s even cooler is that you can utilize the same process in a TFS build with little work at all!  Below shows the simple steps to creating a build definition that will deploy your web application.

1.  Install Web Deploy on the IIS server you wish to deploy to. http://go.microsoft.com/?linkid=9684516 for x86 or http://go.microsoft.com/?linkid=9684517 for x64.  This is used to remotely install you web application and configure any IIS settings.  NOTE:  I recently ran into some issues with installing this where it doesn’t install all the necessary components.  If you reinstall it and select Change, you may find that all component where not installed.  If you are running into issues with permissions during web deploy see this thread http://william.jerla.me/post/2010/03/20/Configuring-MSDeploy-in-IIS-7.aspx.

2.  Create a new build definition.  This can be found on the Team Explorer windows, and right clicking the Builds folder.

3.  Set a drop location for the build output.

image

4.  Set the following MSBuild Arguments. These are required for the web deploy build target. These are space separated build parameters. Four of them you must provide information for.  If you don’t provide information about username password, I believe it will use the build agent’s process identity.

/p:DeployOnBuild=True

/p:DeployTarget=MsDeployPublish

/p:MSDeployPublishMethod=RemoteAgent

/p:CreatePackageOnPublish=True

/p:DeployIISAppPath={Site/application}

/p:MsDeployServiceUrl={You IIS Server Running Web Deploy Agent}

/p:UserName={Username}

/p:Password={Password}

{Site/application} – the web sites name of application name under the site. An example of just the site would be “Default Web Sites”, or in the case of a application under a site then “Default Web Sites/MyApp”.

{You IIS Server Running Web Deploy Agent} – the server name of IP address of the IIS server that your deploying to. It will be running the Web Deploy Agent service.  For example, http://MyIISMachine or http://10.210.7.123.

{Username} – a username that has sufficient privileges on the report IIS server copy content and modify IIS metabase.

{Password} – password for the user

image

5.  You might have to place the build agent account in the Adminstrators group on the machine being deployed to.

6.  Save you build definition.  Now queue it and watch it run.

See also my post on Configure Click-Once Publisher in Visual Studio.

Configure Click-Once Publisher in Visual Studio

Click once publishing makes it easy to deploy web applications right within Visual Studio.  Some hosting providers, like Discount ASP.NET, support click once publishing.  If you are using a hosting provider, check with them for the required information as they will provide you with the correct credentials and service url.

1.  Highlight your web application that you are deploying. Right click it and select Publish.

image

2.  The Publish Web dialog should appear. Set the following items:

· Publish Method = Web Deploy – this tells it to use the web deploy agent that will be running on our remote IIS server.

· Service Url – set this to the server name/IP of the machine that you are deploying to, which also has the web deploy agent running on it. If you are deploying to a hosting provider and they support this, check with them for this configuration.

· Site/application – set this to the site name and/or site/application name. If you don’t have applications under your site then this can just be the site name, like “Default Web Site”.

· Username and Password – set the credentials with sufficient privileges to the deployment operations, this user will need to be able to edit the IIS metabase. If you are using a hosting provider, they will provide this information for you.

image

3.  Click Publish, and that’s it.