<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>MVC on Bryan Avery</title><link>https://bryanavery.co.uk/categories/mvc/</link><description>Recent content in MVC on Bryan Avery</description><generator>Hugo -- gohugo.io</generator><language>en-GB</language><copyright>© 2026 Bryan Avery.</copyright><lastBuildDate>Thu, 08 Dec 2016 16:51:32 +0000</lastBuildDate><atom:link href="https://bryanavery.co.uk/categories/mvc/index.xml" rel="self" type="application/rss+xml"/><item><title>Kendo Grid custom sort order</title><link>https://bryanavery.co.uk/kendo-grid-custom-sort-order/</link><pubDate>Thu, 08 Dec 2016 16:51:32 +0000</pubDate><guid>https://bryanavery.co.uk/kendo-grid-custom-sort-order/</guid><description>&lt;p&gt;I’ve been racking my brains as the sorting on the Kendo Grid is pretty good, only that I need to be able to not sort in the order that is being displayed.  An example would be numbers written out in full and you want them in number sort order.&lt;/p&gt;</description></item><item><title>The way to handle unauthorised requests to Ajax actions in ASP.NET MVC</title><link>https://bryanavery.co.uk/the-way-to-handle-unauthorised-requests-to-ajax-actions-in-asp-net-mvc/</link><pubDate>Thu, 18 Dec 2014 10:12:00 +0000</pubDate><guid>https://bryanavery.co.uk/the-way-to-handle-unauthorised-requests-to-ajax-actions-in-asp-net-mvc/</guid><description>&lt;h2&gt;Problem&lt;/h2&gt;&#10;&lt;p&gt;I have created a view that posts to an action via Ajax with the expectation that the action will return the requested data or an empty string.  Even better, I would like it to be configurable to return whatever value I see fit.&lt;/p&gt;</description></item><item><title>Active links on Bootstrap Navbar with ASP.NET MVC</title><link>https://bryanavery.co.uk/active-links-on-bootstrap-navbar-with-asp-net-mvc/</link><pubDate>Mon, 20 Jan 2014 14:06:00 +0000</pubDate><guid>https://bryanavery.co.uk/active-links-on-bootstrap-navbar-with-asp-net-mvc/</guid><description>&lt;p&gt;If you have used &lt;a href="http://getbootstrap.com/" target="_blank"&gt;Bootstrap&lt;/a&gt; with your ASP.NET MVC application, you might have faced some issues with implementing active links of it’s Navbar component. We’ll have to dynamically add a css class called active to the particular menu item in order to make it selected in the Navbar. Here is a HtmlHelper extension which is capable of rendering menu items as well as drop-down menu items. I’ve used ASP.NET MVC 5 with Razor and Bootstrap 3.&lt;/p&gt;</description></item><item><title>Make Fake MVC Objects for testing</title><link>https://bryanavery.co.uk/make-fake-mvc-objects-for-testing/</link><pubDate>Thu, 19 Dec 2013 11:53:00 +0000</pubDate><guid>https://bryanavery.co.uk/make-fake-mvc-objects-for-testing/</guid><description>&lt;p&gt;If you need to create some Fake MVC objects for testing MVC objects then here you are:&lt;/p&gt;&#10;&lt;h2&gt;FakeControllerContext&lt;/h2&gt;&#10;&lt;pre class="brush: c-sharp;"&gt;using System.Collections.Specialized;&#10;using System.Web;&#10;using System.Web.Mvc;&#10;using System.Web.Routing;&#10;using System.Web.SessionState;&#10;&lt;p&gt;using MvcContrib.TestHelper.Fakes;&lt;/p&gt;&#10;&lt;p&gt;public class FakeControllerContext : ControllerContext&#10;{&#10;public FakeControllerContext(IController controller)&#10;: this(controller, null, null, null, null, null, null)&#10;{&#10;}&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;public FakeControllerContext(IController controller, HttpCookieCollection cookies)&#10; : this(controller, null, null, null, null, cookies, null)&#10;{&#10;}&#10;&#10;public FakeControllerContext(IController controller, SessionStateItemCollection sessionItems)&#10; : this(controller, null, null, null, null, null, sessionItems)&#10;{&#10;}&#10; &#10;public FakeControllerContext(IController controller, NameValueCollection formParams)&#10; : this(controller, null, null, formParams, null, null, null)&#10;{&#10;}&#10; &#10;public FakeControllerContext(IController controller, NameValueCollection formParams, NameValueCollection queryStringParams)&#10; : this(controller, null, null, formParams, queryStringParams, null, null)&#10;{&#10;}&#10; &#10;public FakeControllerContext(IController controller, string userName)&#10; : this(controller, userName, null, null, null, null, null)&#10;{&#10;}&#10; &#10;public FakeControllerContext(IController controller, string userName, string[] roles)&#10; : this(controller, userName, roles, null, null, null, null)&#10;{&#10;}&#10; &#10;public FakeControllerContext&#10; (&#10; IController controller,&#10; string userName,&#10; string[] roles,&#10; NameValueCollection formParams,&#10; NameValueCollection queryStringParams,&#10; HttpCookieCollection cookies,&#10; SessionStateItemCollection sessionItems&#10; )&#10; : base(new FakeHttpContext(new FakePrincipal(new FakeIdentity(userName), roles), formParams, queryStringParams, cookies, sessionItems), new RouteData(), (Controller)controller)&#10;{ }&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;}&lt;/pre&gt;</description></item><item><title>Disclaimer Page using MVC</title><link>https://bryanavery.co.uk/disclaimer-page-using-mvc/</link><pubDate>Wed, 18 Dec 2013 13:17:00 +0000</pubDate><guid>https://bryanavery.co.uk/disclaimer-page-using-mvc/</guid><description>&lt;p&gt;If you ever need a disclaimer page in MVC, there are a few things you need to consider, the first is you should not be able to get to a page without agreeing to the disclaimer and if you have to re-login then you need to be presented with the disclaimer page again.&lt;/p&gt;</description></item><item><title>URL Activity Tampering and how to prevent it in MVC</title><link>https://bryanavery.co.uk/url-activity-tampering-and-how-to-prevent-it-in-mvc/</link><pubDate>Wed, 30 Oct 2013 13:04:00 +0000</pubDate><guid>https://bryanavery.co.uk/url-activity-tampering-and-how-to-prevent-it-in-mvc/</guid><description>&lt;p&gt;No web applications are developed without concerning the security issues. But it is not sure whether the security measures are implemented correctly and the applications are well protected. We could see a lot of web applications query string parameters in plain string which are easy to manipulate and way to escalate restricted resources. Even the URL parameters are encrypted there is still a chance of a bruteforce attack by manipulating the encrypted hash and it yields when the database is large where the chance of matching a random hash is high. So it is clear the importance to protect the URL in the web application is necessary for all programming platforms. Here, this is discussed with reference to ASP.NET MVC and the utility class is presented.&lt;/p&gt;</description></item><item><title>URL Validator</title><link>https://bryanavery.co.uk/url-validator/</link><pubDate>Fri, 25 Oct 2013 08:14:00 +0000</pubDate><guid>https://bryanavery.co.uk/url-validator/</guid><description>&lt;p&gt;I needed to check to see if a URL that the user was entering was a valid Url, does it contain HTTP:// or HTTPS:// I ended up writing a small ValidationAttribute to add to the collection I am now building up, here is the code:&lt;/p&gt;</description></item><item><title>MVC way to check the File Extension</title><link>https://bryanavery.co.uk/mvc-way-to-check-the-file-extension/</link><pubDate>Fri, 04 Oct 2013 10:07:00 +0000</pubDate><guid>https://bryanavery.co.uk/mvc-way-to-check-the-file-extension/</guid><description>&lt;p&gt;Following on from my article about checking for the &lt;a href="https://bryanavery.co.uk/post/2013/06/25/Determine-which-file-type-has-been-uploaded/"&gt;file signature&lt;/a&gt; to ensure that the file is who it says it is&lt;/p&gt;&#10;&lt;p&gt;I decide to take it further and produce a Data Validation attribute, I found that the Microsoft.Web.Mvc.dll library already has one ,but it only checks for the file names, which is generally okay.&lt;/p&gt;</description></item><item><title>MVC running on IIS8</title><link>https://bryanavery.co.uk/mvc-running-on-iis8/</link><pubDate>Mon, 08 Jul 2013 20:46:00 +0000</pubDate><guid>https://bryanavery.co.uk/mvc-running-on-iis8/</guid><description>&lt;p&gt;After a number of 500 errors coming up from IIS8 under Windows Server 2012, I final found the solution.&lt;/p&gt;&#10;&lt;p&gt;The solution was to go back to the Microsoft Server 2012 Dashboard and select the “Add Roles” feature. Scroll down to the root “Web Server (IIS)”, expand “Web Server”, expand “Application Development” and select “ASP.NET 3.5” and “ASP.NET 4.5” and “Install”.&lt;/p&gt;</description></item><item><title>Validating an e-mail address the DataAnnotation way</title><link>https://bryanavery.co.uk/validating-an-e-mail-address-the-dataannotation-way/</link><pubDate>Thu, 27 Jun 2013 15:31:00 +0000</pubDate><guid>https://bryanavery.co.uk/validating-an-e-mail-address-the-dataannotation-way/</guid><description>&lt;p&gt;There are many ways to validate if an e-mail address is right, and many wrong ways.  The biggest wrong way is using a Regular expression, why?  They are complex and there is not one that resolves this issue, you will always get an invalid e-mail address that passes through okay.&lt;/p&gt;</description></item><item><title>Determine which file type has been uploaded</title><link>https://bryanavery.co.uk/determine-which-file-type-has-been-uploaded/</link><pubDate>Tue, 25 Jun 2013 15:04:00 +0000</pubDate><guid>https://bryanavery.co.uk/determine-which-file-type-has-been-uploaded/</guid><description>&lt;p&gt;It is quite an easy process to upload files from a web browser, but how do you know it is the correct type of document you are asking for?&lt;/p&gt;&#10;&lt;p&gt;You can check the file extenstion, but a hacker would just change the file type to another extension, so this is not a fail safe way of checking the file type being uploaded.&lt;/p&gt;</description></item><item><title>Stay Alive in MVC</title><link>https://bryanavery.co.uk/stay-alive-in-mvc/</link><pubDate>Mon, 17 Jun 2013 13:10:00 +0000</pubDate><guid>https://bryanavery.co.uk/stay-alive-in-mvc/</guid><description>&lt;p&gt;Have you ever opened a page for one of your websites and it lags for a while before it finally shows a page but then all of your following requests are quick? If you were to Google the problem you’d find that often it ends up having to do with &lt;a href="https://www.google.com/search?q=asp.net+site+starts+slow&amp;amp;safe=active" target="_blank"&gt;IIS meeting an idle time limit&lt;/a&gt; and shuts down your site.&lt;/p&gt;</description></item><item><title>Cross Site Attacks – XSS</title><link>https://bryanavery.co.uk/cross-site-attacks-xss/</link><pubDate>Wed, 15 May 2013 11:32:00 +0000</pubDate><guid>https://bryanavery.co.uk/cross-site-attacks-xss/</guid><description>&lt;div class="ExternalClass6794FBB9115D4A3A84D5F1E5C83E2E41"&gt;Cross Site Attacks (XSS) what is that all about?&lt;/div&gt;&#10;&lt;div class="ExternalClass6794FBB9115D4A3A84D5F1E5C83E2E41"&gt; &lt;/div&gt;&#10;&lt;div class="ExternalClass6794FBB9115D4A3A84D5F1E5C83E2E41"&gt;“Cross-site scripting (XSS) is a type of computer security vulnerability typically found in Web applications. XSS enables attackers to inject client-side script into Web pages viewed by other users. A cross-site scripting vulnerability may be used by attackers to bypass access controls such as the same origin policy. Cross-site scripting carried out on websites accounted for roughly 84% of all security vulnerabilities documented by Symantec as of 2007. Their effect may range from a petty nuisance to a significant security risk, depending on the sensitivity of the data handled by the vulnerable site and the nature of any security mitigation implemented by the site’s owner.”&lt;/div&gt;&#10;&lt;div class="ExternalClass6794FBB9115D4A3A84D5F1E5C83E2E41"&gt; &lt;/div&gt;&#10;&lt;div class="ExternalClass6794FBB9115D4A3A84D5F1E5C83E2E41"&gt;All user’s inputs can be a back door for attacker to attack your site. User’s inputs that we will categorize as un-trusted inputs are :&lt;/div&gt;&#10;&lt;div&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Incoming URLs including Request.QueryString[] values&lt;/li&gt;&#10;&lt;li&gt;Form post data (Request.Form[] values including values from hidden fields and disabled fields)&lt;/li&gt;&#10;&lt;li&gt;Cookies&lt;/li&gt;&#10;&lt;li&gt;Data in HTTP Headers (such as Request.UserAgent and Request.UrlReferrer).&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/div&gt;&#10;&lt;div&gt;Your site could be attacked by altering the query string, form values, or cookies data. The solution is not to prevent request manipulation but to check that each request is a legal request for the logged-in visitor.&lt;/div&gt;&#10;&lt;h2 class="ExternalClass6794FBB9115D4A3A84D5F1E5C83E2E41"&gt;&lt;span style="color: #000000;"&gt;&lt;strong&gt;Cross-Site Scripting and HTML Injection&lt;/strong&gt;&lt;/span&gt;&lt;/h2&gt;&#10;&lt;div class="ExternalClass6794FBB9115D4A3A84D5F1E5C83E2E41"&gt;If an attacker can get our site to return some javascript to our visitors, then the attacker’s script can take control of our visitors’ browsing session and by this the attackers can collect the personal information (passwords, credit card details) of our visitors. There are two main ways an attacker follow to get this:&#10;&lt;ol&gt;&#10;&lt;li&gt;&lt;em&gt;Persistently&lt;/em&gt;:  by entering formed malicious input into some feature and hoping we will store it into our database and then issue it back to other visitors.&lt;/li&gt;&#10;&lt;li&gt;&lt;em&gt;Non-persistently or Passively &lt;/em&gt;: by finding a way of sending malicious data in a request to our application and having our application echo that data back in it is response. The attacker then finds a way to trick a victim into making such a request.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;/div&gt;&#10;&lt;h2 class="ExternalClass6794FBB9115D4A3A84D5F1E5C83E2E41"&gt;&lt;strong&gt;&lt;span style="color: blue;"&gt;&lt;span style="color: #000000;"&gt;Razor HTML Encoding&lt;/span&gt;&lt;/span&gt;&lt;/strong&gt;&lt;/h2&gt;&#10;&lt;div class="ExternalClass6794FBB9115D4A3A84D5F1E5C83E2E41"&gt;The Razor view engine helps protect us against XSS attacks by encoding any data that we refer to using the @ tag to make it safe to display as HTML. This means when we request an URL with a javascript code as a query string, Razor processes the query string value and replaces the special characters and rendering javascript code as a simple text.&#10;&lt;p&gt;Razor view engine treats the contents of MvcHtmlString objects as if they were encoded, even when that is not the case. We can use Html.Raw helper method to include our HTML into the web page without it being encoded.&lt;/p&gt;&lt;/div&gt;</description></item><item><title>Preventing Cross Site Request Forgery</title><link>https://bryanavery.co.uk/preventing-cross-site-request-forgery/</link><pubDate>Wed, 15 May 2013 09:41:00 +0000</pubDate><guid>https://bryanavery.co.uk/preventing-cross-site-request-forgery/</guid><description>&lt;p&gt;What is Cross Site Request Forgery (CSRF)?&lt;/p&gt;&#10;&lt;p&gt;“A CSRF attack forces a logged-on victim’s browser to send a pre-authenticated request to a vulnerable web application, which then forces the victim’s browser to perform a hostile action to the benefit of the attacker. CSRF can be as powerful as the web application that it attacks.”&lt;/p&gt;</description></item><item><title>How to test “Only certain roles should have access to a controller” in MVC</title><link>https://bryanavery.co.uk/how-to-test-only-certain-roles-should-have-access-to-a-controller-in-mvc/</link><pubDate>Tue, 30 Apr 2013 09:42:00 +0000</pubDate><guid>https://bryanavery.co.uk/how-to-test-only-certain-roles-should-have-access-to-a-controller-in-mvc/</guid><description>&lt;p&gt;Using the &lt;a href="http://en.wikipedia.org/wiki/Test-driven_development" target="_blank"&gt;TDD&lt;/a&gt; approach in an MVC application, I was wondering how can you ensure that only certain roles have access to the controller.  Why should we test for this?  Because this is a clearly defined business rule and all business rules need to have Unit Tests where possible.&lt;/p&gt;</description></item><item><title>Encrypting Web.config for the website</title><link>https://bryanavery.co.uk/encrypting-web-config-for-the-website/</link><pubDate>Thu, 17 Jan 2013 16:57:00 +0000</pubDate><guid>https://bryanavery.co.uk/encrypting-web-config-for-the-website/</guid><description>&lt;p&gt;Security is always high for installation to customer so is it not about time you encripted the connection string in your web.config file?&lt;/p&gt;&#10;&lt;p&gt;This is how it looks like before encrypting:&lt;/p&gt;&#10;&lt;pre class="brush: xml;"&gt;&amp;lt;connectionStrings&amp;gt;&#10; &amp;lt;add name="Pubs" connectionString="Server=localhost;Integrated Security=True;Database=Pubs"&#10; providerName="System.Data.SqlClient" /&amp;gt;&#10; &amp;lt;add name="Northwind" connectionString="Server=localhost;Integrated Security=True;Database=Northwind"&#10; providerName="System.Data.SqlClient" /&amp;gt;&#10;&amp;lt;/connectionStrings&amp;gt;&lt;/pre&gt;&#10;&lt;p&gt;We can encrypt any section of your Web.config file on-the-fly and programatically. If you have full access to your Web server, you can encrypt your connection strings with this single command-line located in the in the %windows%\Microsoft.NET\Framework\versionNumber folder:&lt;/p&gt;</description></item><item><title>Upgrading an ASP.NET MVC 3 Project to ASP.NET MVC 4</title><link>https://bryanavery.co.uk/upgrading-an-asp-net-mvc-3-project-to-asp-net-mvc-4/</link><pubDate>Sun, 09 Dec 2012 09:46:00 +0000</pubDate><guid>https://bryanavery.co.uk/upgrading-an-asp-net-mvc-3-project-to-asp-net-mvc-4/</guid><description>&lt;p&gt; &lt;/p&gt;&#10;&lt;p&gt;ASP.NET MVC 4 can be installed side by side with ASP.NET MVC 3 on the same computer, which gives you flexibility in choosing when to upgrade an ASP.NET MVC 3 application to ASP.NET MVC 4.&lt;/p&gt;</description></item><item><title>Using MEF in MVC to inject Controllers</title><link>https://bryanavery.co.uk/using-mef-in-mvc-3/</link><pubDate>Mon, 12 Mar 2012 10:01:00 +0000</pubDate><guid>https://bryanavery.co.uk/using-mef-in-mvc-3/</guid><description>&lt;p&gt;After travelling for a year I was interest to see if MEF would work with MVC, and after some digging around I found it can.&lt;/p&gt;&#10;&lt;p&gt;***UPDATED****&lt;/p&gt;&#10;&lt;p&gt;Added a CastleWindsor example&lt;/p&gt;&#10;&lt;p&gt;With any MEF you’ll need a Composition Factory which in this case I’ll place in the MEF directory, but it can be in a different project if required.&lt;/p&gt;</description></item><item><title>Upgrading from MVC 2.0 to MVC 3.0</title><link>https://bryanavery.co.uk/upgrading-from-mvc-2-0-to-mvc-3-0/</link><pubDate>Fri, 17 Feb 2012 15:01:00 +0000</pubDate><guid>https://bryanavery.co.uk/upgrading-from-mvc-2-0-to-mvc-3-0/</guid><description>&lt;p&gt;Have you tried to upgrade an MVC 2.0 application to MVC 3.0, it’s not easy as you need to change a lot of configuration.&lt;/p&gt;&#10;&lt;p&gt;Well &lt;a href="http://weblogs.asp.net/leftslipper/" target="_blank"&gt;Eilon Lipton&lt;/a&gt; has a codeplex project to make life easier &lt;a href="http://aspnet.codeplex.com/releases/view/59008" target="_blank"&gt;ASP.NET MVC 3 Application Upgrader&lt;/a&gt;. This is a standalone application that takes the troubles away for you and delivers an MVC 3.0 application&lt;/p&gt;</description></item><item><title>ASP.NET MVC Routing checker</title><link>https://bryanavery.co.uk/asp-net-mvc-routing-checker/</link><pubDate>Thu, 02 Feb 2012 10:16:00 +0000</pubDate><guid>https://bryanavery.co.uk/asp-net-mvc-routing-checker/</guid><description>&lt;p&gt;I came across this nice little MVC routing checker by Phil Haacket&lt;/p&gt;&#10;&lt;p&gt;&lt;a href="http://haacked.com/archive/2008/03/13/url-routing-debugger/" target="_blank"&gt;http://haacked.com/archive/2008/03/13/url-routing-debugger/&lt;/a&gt;&lt;/p&gt;&#10;&lt;p&gt;Simple to use just add&lt;/p&gt;&#10;&lt;pre class="brush: c-sharp;"&gt;RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);&lt;/pre&gt;&#10;&lt;p&gt;in to the Application_Start of your Global.asax and it does all the reporting on all your Routings&lt;/p&gt;</description></item></channel></rss>