Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sitecore and ASP.net MVC

We are starting off a new project with sitecore as our CMS. I was thinking of using Sitecore as the Content Authoring Tool and use ASP.net MVC as in the Content delivery(CDA) Side along with Sitecore. Would love to hear your ideas and thoughts on this.

Have anybody tried this?

Is sitecore and MVC competing or Complementing technologies?

Any architectural ideas are welcome.

like image 651
Vinod Avatar asked Aug 20 '09 19:08

Vinod


People also ask

Does Sitecore use MVC?

A Sitecore provided standard controller will automatically build a model using the context of the current request. If a layout (visual file) has been assigned to the item (content), and the layout is using Razor, Sitecore will automatically switch to MVC and automatically provide the full model to the view file.

What is MVC in Sitecore?

Sitecore MVC offers native support for the model-view-controller programming model. It operates alongside ASP.Net Web Forms on a per request basis. This means that it is possible to have a website where some pages are handled by MVC and others are handled by Web Forms.

What is Sitecore in asp net?

Sitecore is one of the leading enterprise-level content management systems built on ASP.NET, enabling web content editors and marketers to have full control over all aspects of their website from social integration and blog posts to advanced personalisation, ecommerce and more.

Does Sitecore use .NET core?

1.2 ASP.NET Core Although some features of Sitecore prior Sitecore 10 are based on ASP.NET Core, the core rendering of pages and components was always dependent on .


2 Answers

I think the real question you should be asking here is; if you already have Sitecore in place - why would you want the overhead and complicatino of introducing MVC?

Do you have any business requirements outside the basic web site that would necessitate MVC?

like image 35
Mark Cassidy Avatar answered Oct 06 '22 15:10

Mark Cassidy


For certain cases, there can be huge benefit to merging the two. MVC isn't that great of a fit for content-driven sites. However, web applications with structured flow and multiple presentations of data benefit hugely from it. Sitecore has somewhat of a limitation when it comes to multiple presentations of data -- you can only define one set of design details on an item. If you don't have requirements for WYSIWYG editing or easy one-click preview, you can use Sitecore as a data repository, and take advantage of some of the Context values that come from its pipeline (such as language).

A couple modifications are necessary to the Sitecore HTTP pipeline to make this work:

1) If using the aspx extension in IIS6 to get ASP.NET to handle MVC requests (e.g. /Controller.aspx/Action), fix Sitecore's FilePath parsing (there is a bug in how Sitecore resolves the FilePath that will result in the path getting chopped).

To fix this, place a new processor at the start of the httpRequestBegin pipeline.

public class MvcFixHttpProcessor : Sitecore.Pipelines.HttpRequest.HttpRequestProcessor {     public override void Process(Sitecore.Pipelines.HttpRequest.HttpRequestArgs args)     {         //when using a path such as /Controller.aspx/Blahblahblah, Sitecore's parsing of FilePath can break if Blahblahblah is too long         RouteData routeData = RouteTable.Routes.GetRouteData(new HttpContextWrapper(args.Context));         if (routeData != null)         {             args.Url.FilePath = args.Context.Request.Url.LocalPath;         }     } } 

(Edit 9/13/2011: I haven't had to use the above fix in some time.)

2) Tell Sitecore to ignore URLs that are routed to ASP.NET MVC

To accomplish this, place a new processor in the httpRequestBegin pipeline following the ItemResolver.

public class SystemWebRoutingResolver : Sitecore.Pipelines.HttpRequest.HttpRequestProcessor {     public override void Process(Sitecore.Pipelines.HttpRequest.HttpRequestArgs args)     {         RouteData routeData = RouteTable.Routes.GetRouteData(new HttpContextWrapper(args.Context));         if (routeData != null)         {             args.AbortPipeline();         }     } } 

If using languages in your Sitecore URLs, you will need to add some custom logic that combines Sitecore link generation with MVC ActionLink generation, to ensure that language is added to the start of your MVC URL. However with the pipeline modifications above, the addition of language to the URL should have no side effects on MVC routing (because Sitecore rewrites the URL after reading the language).

Once again, this approach is only useful for certain types of applications. In those cases though, Sitecore makes an excellent data layer for your model. Look into creating custom Item wrappers to create strongly-typed domain objects based on Sitecore Items.

(Edit 9/13/2011: Custom Item Generator works great for this. http://blog.velir.com/index.php/2010/10/19/custom-item-generator/)

Best of luck.

like image 192
nickwesselman Avatar answered Oct 06 '22 15:10

nickwesselman