Author Archives: Sharavan Kumar Kasagoni

About Sharavan Kumar Kasagoni

Shravan is working as Senior Development Engineer in Pramati Technologies, earlier he worked with Thomson Reuters. He is responsible for design and development of RESTful APIs, Single Page Web Applications. He is awarded as Microsoft Microsoft Most Valuable Professional for years 2016, 2015, 2014 2013 and 2012 from Microsoft. He also is a Microsoft Certified Professional for Programming in HTML5 with JavaScript and CSS3 Specialist. Programming is his passion. He is core member of Microsoft User Group Hyderabad. In his overall experience of 7+ years, his expertise includes Azure, C#, Swift, ASP.NET MVC, HTML 5, TypeScript, Object Oriented JavaScript, JavaScript Frameworks (Angular, Angular 2, Backbone, React), iOS. He also experiments on next generation JavaScript and Web technologies. Shravan's Blog: http://theshravan.net/blog Follow Shravan @ Twitter: @techieshravan

How to allow user to input html in ASP.NET MVC?

Whenever we submit HTML or JavaScript as input in ASP.NET MVC application we get an exception like "A potentially dangerous Request.Form value was detected from the client (……)”. Because ASP.NET MVC has built-in request validation that helps you automatically protect against cross-site scripting (XSS) attacks and HTML injection attacks, it will prevent the user from posting HTML or JavaScript as input.

Continue reading

How to return 404 Http status code from ASP.NET MVC Application?

ASP.NET MVC3 includes a new class HttpNotFoundResult in  System.Web.Mvc namespace.

HttpNotFoundResult: Instance of HttpNotFoundResult class indicates to client(browser) that the requested resource was not found. It returns a 404 HTTP status code to the client. Generally we return 404 status code if a requested webpage is not available. In case of MVC applications we return 404 status code is in terms of resources, for example we are searching for particular user profile in the portal, if the user profile is not found, we can return 404.

Continue reading

How To Create Session-less Controller in MVC3

How to manage the controller’s session state?

Simply we can decorate the controller class with “SessionState” attribute. [SessionState()] attribute accepts SessionStateBehaviour enumeration.

SessionStateBehaviour enumeration has the following constants.

  • SessionStateBehavior.Default – ASP.NET default logic is used to determine the session state behavior for the request.
  • SessionStateBehavior.Required – Full read-write session state behavior is enabled for the request.
  • SessionStateBehavior.ReadOnly – Read only session state is enabled for the request.
  • SessionStateBehavior.Disabled – Session state is not enabled for processing the request.

Continue reading

Creating Custom HTML Helpers in ASP.NET MVC

ASP.NET MVC provides many built-in HTML Helpers.  With help of HTML Helpers we can reduce the amount of typing of HTML tags for creating a HTML page.

For example we use Html.TextBox() helper method it generates html input textbox. Write the following code snippet in MVC View:

<%=Html.TextBox("txtName",20)%>

It generates the following html in output page: <input id=”txtName” name=”txtName” type=”text” value=”20″ />

List of built-in HTML Helpers provided by ASP.NET MVC.

  • ActionLink() – Links to an action method.
  • BeginForm() – Marks the start of a form and links to the action method that renders the form.
  • CheckBox() – Renders a check box.
  • DropDownList() – Renders a drop-down list.
  • Hidden() – Embeds information in the form that is not rendered for the user to see.
  • ListBox() – Renders a list box.
  • Password() – Renders a text box for entering a password.
  • RadioButton() – Renders a radio button.TextArea() – Renders a text area (multi-line text box).
  • TextBox () – Renders a text box.

How to develop our own Custom HTML Helpers?

For developing custom HTML helpers the simplest way is to write an extension method for the HtmlHelper class. See the below code, it builds a custom Image HTML Helper for generating image tag.

using System;
using System.Web.Mvc;
namespace MvcHelpers
{
    public static class CustomHtmlHelpers
    {
        public static TagBuilder Image(this HtmlHelper helper, string imageUrl, string alt)
        {
            if (!String.IsNullOrEmpty(imageUrl))
            {
                TagBuilder imageTag = new TagBuilder("img");
                imageTag.MergeAttribute("src", imageUrl);
                imageTag.MergeAttribute("alt", alt);
                return imageTag;
            }
            return null;
        }
    }
}

The next step is in order to make it available to Views, we should include the name space of out custom HTML helper class into Web.config

Build the solution to make sure the helper method is available to views. Goto any view in the solution access the newly created HTML image helper method.

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
      <title>Home</title>
</head>
<body>
      <div>
            <h2>
                  <%= Html.Encode(ViewData["Message"]) %>
            </h2>
      <%=Html.Image("/Images/logo.png","logo") %>
      </div>
</body>
</html>

<%=Html.Image(“/Images/logo.png”,”logo”) %>

The image HTML helper generates the following HTML.

<img src=”/Images/logo.png” alt=”logo”/>

Image Tag Helper is simple example of HTML helpers, we can put any sort of complex logic into HTML Helpers & access them with in the views with a simple method call. in turn which makes the view simpler.