How to Display HTML with MVC’s NullDisplayText Attribute

Adding DisplayFormat attribute to model’s property allows you to set formatting options for that property’s value. One of options is NullDisplayText, where you can set what to display if the value is null.

Sometimes you may wish to have NullDisplayText return a piece of HTML. To be able to render this HTML correctly, you need to set HtmlEncode value to false.

[DisplayFormat(NullDisplayText = "<div>My HTML<div>", HtmlEncode = false)]
public int? MyProperty { get; set; }

Hide URL Path When Printing in Chrome

When you print (or print preview) web page in Chrome, printout will include URLs for each link on the page.

For example, if you have a link “Back to Homepage”, when printed it will display as “Back to Homepage (www.mysite.com)”.

To stop URLs being printed, add the following style –

 <style media="print">
    a[href]:after
    {
        content:none !important;
    }
</style>

ASP.NET MVC – Prevent Browser Back Button Openning Page After Logout

If you have ASP.NET MVC application with login / logout functionality (for example using Forms Authentication), you may have noticed that after logout, if you hit back button in your browser, last page you were on will open. In secure application this could present a problem, as it could expose information that only authorized users should view.

Culprit in this scenario is our browser, as it will quite happily show cached version of the page after you’ve logged out.

There isn’t much you can do, except tell your browser to not cache your application’s pages. You can do this for individual pages in your applications, or if you want all pages not to be cached, you can add an event in Global.asax file.

Add Application_BeginRequest() method to your application’s Global.asax file, and add code below which will disable browser caching for each request.

        protected void Application_BeginRequest()
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
            Response.Cache.SetNoStore();
        }

Visual Studio – Enable Debug for new Build Configuration

When you are setting up new build configuration which you want to be able to debug, you need to set Debug info to Full.

Right click the project in Solution Explorer and select Properties. Select build tab and then select desired configuration from Configuration drop down list at the top of the page.

Click on Advanced at the bottom of output section and select Full from Debug Info drop down list.

Save changes to project settings and rebuild your solution.

VSBuildConfigDebug

Font Awesome and IIS

If you are using Font Awesome in your ASP.Net web app which is hosted on IIS, you need to set appropriate mime type for Font Awesome files, otherwise IIS won’t know how to serve them.

To do this, you’ll need to add mime type mapping to <system.webServer> section in your application’s Web.config file.

Before registering each file extension (woff and woff2), we’ll want to remove them first, just in case that mime types for those are already registered on IIS host.

<system.webServer>
<staticContent>
<remove fileExtension=”.woff” />
<mimeMap fileExtension=”.woff” mimeType=”application/x-font-woff” />
<remove fileExtension=”.woff2″ />
<mimeMap fileExtension=”.woff2″ mimeType=”application/x-font-woff2″ />
</staticContent>
</system.webServer>

How to specify template parameter values in SQL Server Management Studio

When using templates in SQL Server Management studio (for example using a template to create new stored procedure), there is always a bunch of parameters for which you need to specify values. You can always go manually through the whole template script and set parameter values, but there is an easier way.

Press Shift+Ctrl+M and parameter value editor will open up. Enter values for each parameter in the list and you are done.

Params

How to enable remote connections to MySQL server

When you install MySQL, remote connections are disabled by default for security reasons.

You can enable remote connections by changing configuration options in config file.

Edit my.cnf configuration file. On Debian based distributions (also includes Raspbian, which is what I’m using), config file is located in /etc/mysql/my.cnf.

sudo pico /etc/mysql/my.cnf

Once config file is open, find section [mysqld]. Under that section you’ll need to set bind address (which by default should be set to 127.0.0.1) to your server’s IP address. You can also bind to 0.0.0.0 if you don’t wish to specify IP address – useful if machine on which MySQL is hosted has dynamically allocated IP address. You’ll also need to comment out or remove skip-networking line if its in the config file.

So if the machine on which MySQL is hosted has an IP address of 192.168.1.115, configuration options need to look like:

[mysqld]
bind-address 192.168.1.115
#skip-networking

Or if you don’t want to specify IP address:

[mysqld]
bind-address 0.0.0.0
#skip-networking

Leave rest of the configuration file as is and save your changes. Restart MySQL for changes to take effect.

Once this is done, you’ll need to enable users to connect remotely – this is done by creating user account(s) with “From Host” option set to specific IP address from which user is going to connect, or setting “From Host” to “%” value which will accept connection from any host for that username.

For more information on user administration, look at MySQL Manual – Users and Privileges or MySQL Manual – Adding User Accounts.