Html Helper for sharing links to Facebook

Wed Jan 12 2011 by MarkP

Over the last few days I have been working on this site to try and improve it whilst I had some time and also to improve my knowledge of the MVC framework. I had a requirement to be able to share my articles with social networking sites such as Facebook. Facebook has a javascript library which simplifies the process of displaying and sharing specific urls or pages on Facebook. Further information and details can be found at the Facebook Share site.

ASP.NET MVC has many built in HtmlHelpers that makes the displaying Html controls simpler and cleaner on the View and reduces the code required especially when the code needs to be dynamically created based on the model or route values. With this in mind a HtmlHelper was created to display the "Share Facebook" button on the page:

  1. public static MvcHtmlString FacebookShare(this HtmlHelper html,string type, string controllerName,string actionName, object routeValues)
  2.         {
  3. var tag = new TagBuilder("a");
  4. tag.MergeAttribute("name", "fb_share");
  5.             
  6. if(!string.IsNullOrEmpty(type))
  7. tag.MergeAttribute("type", type);
  8.  
  9. RequestContext ctx = html.ViewContext.RequestContext;
  10. UrlHelper urlHelper = new UrlHelper(ctx);
  11. tag.MergeAttribute("share_url", urlHelper.Action(actionName, controllerName, new RouteValueDictionary(routeValues), ctx.HttpContext.Request.Url.Scheme, ctx.HttpContext.Request.Url.Host));
  12. return MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal));
  13.         }

This is displayed on the View using the following syntax (using Razor View Engine):

  1. @Html.FacebookShare("Facebook button type", "Controller", "Action", new {RouteValues})

At present the HtmlHelper is limited to Route Values only but could easily be extended to use optional parameters, such as Html attributes or the direct page link if required. The key requirement was to be able to pass in Route Values only and to be able to automatically create the link using MVC's built in methods such as the UrlHelper object.

Update
For Asp.NET MVC 3.0 you would use HtmlString rather than MvcHtmlString and return a new HtmlString(string value) rather than the static Create method exposed in the MvcHtmlString object.

Tags: MVC   Facebook  

ASP.NET Interview Questions

Tue Jan 11 2011 by MarkP

I recently completed a contract and have now been looking at the job market for the last week. Whilst being successful in getting interviews I have realised my interview technique needs improvement and also need to "brush up" on my basic ASP.NET concepts particulary in relation to Web forms development. I realised questions I could answer 2-3 years ago I have struggled with recently. The main reason is that I have spent a lot of time researching new technologies and techniques aswell as working on mainly core systems development and integration - ask me a question of Domain Driven Design or the differences between abstract classes and interfaces and this would be no problem - ask me about the ASP.NET page lifecycle or different caching techniques and I struggle even though I have been working with the technology for many years. With this in mind I have decided to create a list of key points in relation to ASP.NET to ensure I do not fall over on such questions in the future.

Define the ASP.NET Page Life Cycle?

This question comes up frequently and whilst being familar with the core concepts relating to intialisation, page loading and page rendering have forgotten some of the finer details relating to full page life cycle.

Details are as follows:

The page cycle commences when the ProcessRequest() Method is called:

  • Page Pre Initialisation -> Page.PreInit (Access to Master Pages and Themes)
  • Page Initialisation –> Page.Init
  • Initialisation Complete -> Page.InitComplete
  • Viewstate Loading -> Page.LoadViewState
  • Postback Data Processing -> Page.LoadPostData
  • Pre Load -> Page.PreLoad
  • Page Loading -> Page.Load
  • Postback Change Notification -> RaisePostDataChangedEvent
  • Postback Event Handling -> RaisePostBackEvent
  • Load Complete -> Page.LoadComplete
  • Page Pre Rendering Phase -> Page.PreRender
  • Pre Render Complete -> Page.PreRenderComplete
  • View State Saving -> Page.SaveViewState
  • Save Control State -> Page.SaveControlState
  • Page Rendering -> Page.Render
  • Page Unloading -> Page.Unload

Describe the caching techniques available in the ASP.NET Framework?

Key types of caching in a ASP.NET application are : Page Caching (page aspx), Fragment caching (user controls ascx) and Object caching (API) (in memory cache) All cached objects are stored in the AppDomain and are avialable to all users accessing the application. Page and Fragment caching is set using the directive at the top of the aspx page/ascx user control as follows:
@OutputCache Duration="60" VaryByParam="None"

Decribe the different modes Session state can be stored and how it is configured?

Session state is available to each user visiting the application, who can be uniquely identified by the SessionID. The following modes can set for session in the web.config file:

  • InProc mode, which stores session state in memory on the Web server. This is the default.
  • StateServer mode, which stores session state in a separate process called the ASP.NET state service. This ensures that session state is preserved if the Web application is restarted and also makes session state available to multiple Web servers in a Web farm.
  • SQLServer mode stores session state in a SQL Server database. This ensures that session state is preserved if the Web application is restarted and also makes session state available to multiple Web servers in a Web farm.
  • Custom mode, which enables you to specify a custom storage provider.
  • Off mode, which disables session state.

I expect to be adding to this over the next few weeks, to ensure I have all areas covered and not caught out again in the future.

Tags: ASP.NET   Employment