Tuesday, 11 December 2012

Custom MVC routing based on URL stored in database


 Hi,
     here  i am explaining step-by-step dynamic routing (url-rewriting) in MVC3 Razor according to database.
i think this will helpful to you.  



Database
Create two tables in database and insert some records according to below showing....



 CREATE TABLE [dbo].[MvcRoutes](
    [routeID] [int] IDENTITY(1,1) primary key,
    [routeName] [varchar](50) ,
    [routeIsActive] [bit]
)



CREATE TABLE [dbo].[MvcRouteParams](
    [paramID] [int] IDENTITY(1,1) primary key,
    [routeID] [int] references MvcRoutes(routeID),
    [paramKey] [varchar](50) ,
    [paramValue] [varchar](255) ,
    [paramLogicalUrl] [nvarchar](max) )















Global.asax


 public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            SmartSignEntities db = new SmartSignEntities(); // Entity Name
            List<MvcRoutes> dbRoutes = (from b in db.MvcRoutes
                                       where b.routeIsActive == true
                                       orderby b.routeName
                                       select b).ToList();

            foreach (MvcRoutes route in dbRoutes)
            {
                string _id;
                string _controller = "";
                string _action = "";
                string _logicalURL = "";
                foreach (MvcRouteParams param in route.MvcRouteParams)
                {
                    if (param.paramKey.ToLower() == "controller")
                        _controller = param.paramValue;
                    else if (param.paramKey.ToLower() == "id")
                        _id = param.paramValue;
                    else if (param.paramKey.ToLower() == "action")
                        _action = param.paramValue;
                    _logicalURL = param.paramLogicalUrl;
                }
                if (string.IsNullOrEmpty(_logicalURL))
                {
                    _logicalURL = "{" + _controller + "}" + "/{" + _action + "}";
                }

                routes.MapRoute(
                   route.routeName, // Route name
                   _logicalURL, // URL with parameters
                   new { controller = _controller, action = _action, id = UrlParameter.Optional } );

            }

            routes.MapRoute(
               "Default", // Route name
               "{controller}/{action}/{id}", // URL with parameters
               new { controller = "Home", action = "Index", id = UrlParameter.Optional } );
        }


HomeController


  public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View("Index");
        }

        public ActionResult About()
        {
            return View("About");
        }

        public ActionResult ContactUs()
        {
            return View("ContactUs");
        }

        public ActionResult productDetails()
        {
            return View("productDetails");
        }
    }


_Layout.cshtml

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>@ViewBag.Title</title>
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
    <script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script>
</head>
<body>
    <table width="100%" cellpadding="0" cellspacing="0">
        <tr style="font-family: Arial; background-color: Silver; padding: 10px;">
            <td height="50px">
                <h1>
                    Smartsign Design
                </h1>
            </td>
            <td>
                <ul>
                    <li>@Html.ActionLink("ContactUs", "ContactUs", "Home")</li>
                    <li>@Html.ActionLink("About", "About", "Home")</li>
                    <li>@Html.ActionLink("Home", "Index", "Home")</li>
                </ul>

            </td>
        </tr>
    </table>
    @RenderBody()
</body>
</html>



View (About.cshtml)

@{
    ViewBag.Title = "About";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>About</h2>
<br />
<b>
Home ->> About.....
</b>

 
 Url looks like according to database entry

and create other views "ContactUs", "productDetails", "Index" in home controller



Monday, 26 November 2012

Thursday, 11 October 2012

how to check IsNumeric/number values in sql server

CREATE Function Is_numeric(@value varchar(25))
Returns bit
as
Begin
Return
(
    case when @value not like '%[^-0-9.]%' and len(@value)-len(replace(@value,'.',''))<2 and 1= (
    case when charindex('-',@value)>0 then
    case when left(@value,1)='-' and len(@value)-len(replace(@value,'-',''))<2 and len(@value)>1 then
        1
    else
        0
    end
    else
        1
    end
    ) then
        1
    else
        0
    end
    )
End


select dbo.is_numeric('1234')
select dbo.is_numeric('12AB34')
select dbo.is_numeric('ABC')

Friday, 14 September 2012

ASP.NET Life Cycle Overview

How to Block the Page, When Data is Submitting.

Introduction:
To Block the Page when Data is Submitting, we have various options, Either we can use Ajax based UpdateProgress or some jQuery Stuff. But sometime Ajax UpdateProgress not very useful because of complexity. So, the better approch appoach is to use jQuery UI Block Plug-In.
Implementation:
Download jQuery UI Block Plugin from : http://www.malsup.com/jquery/block/
<script type="text/javascript" src="js/jquery.1.4.2.js"></script>
<script type="text/javascript" src="js/jquery.blockUI.js"></script>

  $("#<%= btnSubmit.ClientID%>").click(function() {
 $.blockUI({
                            message: "<h3>Processing, Please Wait...</h3>" ,
                            css: {
                                border: 'none',
                                padding: '15px',
                                backgroundColor: '#000',
                                '-webkit-border-radius': '10px',
                                '-moz-border-radius': '10px',
                                opacity: .5,
                                color: '#fff'
                 }  });

});
The above code is simple code without any AJAX or other complex script.
Ref:  http://www.malsup.com/jquery/block/