Showing posts with label MVC 4.0 Directory. Show all posts
Showing posts with label MVC 4.0 Directory. Show all posts

Sunday, 14 August 2016

Creating Custom Route Constraint in ASP.NET MVC

Posted by : Manav Pandya
Creating Custom Route Constraint in ASP.NET MVC



How to Create Custom route using constraint in ASP.NET MVC ?



  • In following tutorial, we will learn that how to create custom routes using Constraint in ASP.Net MVC.
  • You use route constraints to restrict the browser requests that match a particular route. You can use a regular expression to specify a route constraint.
  • Create New ASP.Net MVC 4 Web Application with “Basic” Project Template.
  • Open [RouteConfig.cs] file from [ App_Start] folder and below [ DateRoute] route exactly above Default route .

Here you can find my demo code :



public static void RegisterRoutes(RouteCollection routes)
{
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            // http://localhost:50399/2014/05/02
                routes.MapRoute(
                name: "DateRoute",
                url: "{year}/{month}/{day}",
                defaults: new { controller = "Blog", action = "Index" },
                constraints: new { year = @"\d{4}", month = @"\d{2}", day = @"\d{2}" }
            );

            // http://localhost:50399/Home/Index
                 routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
}


Description of above Example ASP.NET MVC code :


  • You can see in DateRoute, the URL pattern is [ {year}/{month}/{day} ] and it is using Index action of [ BlogController ].
  • You can also see  “ DateRoute “ contains a regular expression constraint that only matches year/month/day URL pattern.
  • The regular expression ( \d{4} ) matches four integers which is year  and ( \d{2} )matches two integers which is month and day.





Now, Create BlogController and add Index action as shown in below code.

  • BlogController.cs


public class BlogController : Controller

{
        public ActionResult Index(string year, string month, string day)
        {
            ViewBag.date = year + "/" + month + "/" + day;
            return View();
        }
}


Now, you have to create Index.cshtml view by Right click on BlogController Index action ⇒ Add View ⇒ click Add.



It will look like this :

  • Index.cshtml


@{
 ViewBag.Title = "Index";
}

<h2>Index</h2>
<h2>This is the date : @ViewBag.date</h2>


Now, Run the project by pressing “F5” from the keyboard and browse thehttp://localhost:50399/2014/05/02 URl, you will get output as shown in below screenshot. 



I hope you may like my Tutorial

Click here for My more post  :



Read More

Tuesday, 9 August 2016

HTML Helper In ASP.NET MVC

Posted by : Manav Pandya

What is "HTML" in ASP.NET MVC ?

HTML helpers are static extension methods defined in InputExtensions class

These helper methods render HTML maup in view. ASP.Net MVC framework provides different inbuilt helper methods to render different HTML controls.

Below table describes the commonly used HTML helper methods .


Name
Description
@Html.Label("Name")
Display Label
@Html.LabelFor(model=>model.Name)
Display model binded label control
@Html.TextBox("textboxName")
Display textbox control
@Html.TextBoxFor(model=>model.PropertyName)
Display model binded textbox
@Html.TextArea("name")
Display Text area
@Html.CheckBox("name")
Display checkbox
@Html.CheckBoxFor(model=>model.PropertyName)
Display model binded checkbox
@Html.RadioButton("name")
Display radio button
@Html.RadioButtonFor(model=>model.PropertyName)
Display model binded radio button
@Html.Password("name")
Display password field
@Html.PasswordFor(model=>model.PropertyName)
Display model binded password field
@Html.Hidden("name")
Display hidden field
@Html.HiddenFor(model=>model.PropertyName)
Display Model binded hidden field

Read More

Wednesday, 6 July 2016

ASP.NET MVC 4.0 Internet App Directory Structure

Posted by : Manav Pandya

Here I have displayed all folders within single ASP.NET MVC 4.0 Internet Application





Read More