Go to the documentation of this file.00001 using System;
00002
00003 namespace N2.Web.Mvc
00004 {
00010 public class MvcConventionTemplateAttribute : Attribute, IPathFinder
00011 {
00012 private readonly string _otherTemplateName;
00013 private IControllerMapper _controllerMapper;
00014
00015 public string DefaultAction { get; set; }
00016
00017 public MvcConventionTemplateAttribute()
00018 {
00019 DefaultAction = "Index";
00020 }
00021
00027 public MvcConventionTemplateAttribute(string otherTemplateName) : this()
00028 {
00029 _otherTemplateName = otherTemplateName;
00030 }
00031
00032 #region IPathFinder Members
00033
00034 public PathData GetPath(ContentItem item, string remainingUrl)
00035 {
00036 if (remainingUrl != null && (remainingUrl.ToLowerInvariant() == "Default.aspx" || remainingUrl.Contains("/")))
00037 return null;
00038
00039 if (item == null)
00040 throw new ArgumentNullException("item");
00041
00042 Type itemType = item.GetContentType();
00043
00044 string templateName = _otherTemplateName ?? itemType.Name;
00045
00046 string action = remainingUrl ?? DefaultAction;
00047
00048 if (ActionDoesNotExistOnController(item, action))
00049 return null;
00050
00051 return new PathData(item, templateName, action, String.Empty);
00052 }
00053
00054 #endregion
00055
00056
00057 public IControllerMapper ControllerMapper
00058 {
00059 get { return _controllerMapper = _controllerMapper ?? N2.Context.Current.Resolve<IControllerMapper>(); }
00060 set { _controllerMapper = value; }
00061 }
00062
00063 private bool ActionDoesNotExistOnController(ContentItem item, string action)
00064 {
00065 var controllerName = ControllerMapper.GetControllerName(item.GetContentType());
00066
00067 return !ControllerMapper.ControllerHasAction(controllerName, action);
00068 }
00069 }
00070 }