Advertuse

Search This Blog

Your Ad Here
Showing posts with label ASp.NET MVC. Show all posts
Showing posts with label ASp.NET MVC. Show all posts

Tuesday, 12 July 2011

ViewBag v/s ViewData

Hi Friends,
If you go through ASP.NET MVC 3.0 framework then you will find  two frequently  used collection of data object which  used to pass data between control and views  and partial views. as both are doing similar functionality but has  some difference in that , so today , I would like to  put some difference of those and interoperability of that .



ViewData
ViewBag
It is dictionary of name/value pair  collection
It is dynamic  type object
This features exist since 1st release of ASP.NET MVC
This features available from ASP.NET MVC 3.0
This will work  in ASP.NET with .net 3.5 framework and above
This will work well with ASP.NET 4.0  with .net 4.0 framework and above
ViewData is faster compare to ViewBag   
ViewBag is slower than ViewData
While retrieving  data from viewData in to view pages , one need to do proper type casting
While retrieving  data from viewbag in to view pages , one need  not to do  type casting
Hence it make code with lots of  type casting  fragments
Hence it make more clear code

Friday, 3 June 2011

Legacy Url Routing when application move from webform to MVC

// The legacy route class that exposes a RedirectActionNamepublic class LegacyRoute : Route{
    public LegacyRoute(string url, string redirectRuleName):base(url, new LegacyRouteHandler())
    {
        RedirectRuleName = redirectRuleName;
    }

    public string RedirectRuleName { get; set; }

    public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
    {
        return null;
    }
}

 
/ The legacy route handler, used for getting the HttpHandler for the requestpublic class LegacyRouteHandler : IRouteHandler{
    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        return new LegacyHandler();
    }
}

// The legacy HttpHandler that handles the requestpublic class LegacyHandler : IHttpHandler{
    public void ProcessRequest(HttpContext context)
    {
        var requestContext = context.Request.RequestContext;
        string redirectActionName = ((LegacyRoute)requestContext.RouteData.Route).RedirectRuleName;

        var queryString = requestContext.HttpContext.Request.QueryString;
        foreach (var key in queryString.AllKeys)
        {
            requestContext.RouteData.Values.Add(key, queryString[key]);
        }

        VirtualPathData data = RouteTable.Routes.GetVirtualPath(requestContext, redirectActionName, requestContext.RouteData.Values);

        context.Response.Status = "301 Moved Permanently";
        context.Response.AppendHeader("Location", data.VirtualPath);
    }

    public bool IsReusable
    {
        get { return false; }
    }
} 
 
 
 And use rout mapping in global.as
 
routes.MapRoute(
    "Newroutname", // Route name
    "querystringParameter", // URL with parameters
    new { controller = "mycontroller", action = "actionname", id = UrlParameter.Optional } // Parameter defaults);
// redirect /oldurl to the Newroutname route.routes.Add("", new LegacyRoute("oldurl", "Newroutname")); 

 more details  are at   stefanolson Blog




compile by Divyang Panchasara Sr. Programmer Analyst Hitech OutSourcing