我正在尝试创建一个 Umbraco 7 MVC 应用程序。在此过程中,我希望能够创建在幕后管理数据的自定义控制器。通过我的研究,我发现使用 SurfaceController 是最成功的。但是,该路线将“/umbraco/surface/”添加到页面。例如,我的测试控制器和视图看起来像“/umbraco/surface/Test”。有没有办法管理这些路由并让它直接转到“/Test”而不添加 Umbraco 路由?任何有关如何在 Umbraco 7 中创建自定义控制器的指南都会很有帮助!
这就是我在我的项目中取得的成就。挖掘inet我找到了解决方案:
-
In App_Code我创建的文件夹启动.cs有路线:
using System.Web.Mvc;
using System.Web.Routing;
using Umbraco.Core;
namespace mebli
{
public class MyStartupHandler : IApplicationEventHandler
{
public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
//Create a custom routes
// News controller
RouteTable.Routes.MapRoute(
"",
"News",
new
{
controller = "News",
action = "Index",
id = "0"
});
RouteTable.Routes.MapRoute(
"",
"News/Index",
new
{
controller = "News",
action = "Index",
id = "0"
});
RouteTable.Routes.MapRoute(
"",
"News/{id}",
new
{
controller = "News",
action = "Index",
id = UrlParameter.Optional
});
}
public void OnApplicationInitialized(
UmbracoApplicationBase umbracoApplication,
ApplicationContext applicationContext)
{
}
public void OnApplicationStarting(
UmbracoApplicationBase umbracoApplication,
ApplicationContext applicationContext)
{
}
}
}
在我的例子中,这可以让你拥有你想要的路线site.com/新闻, site.com/新闻/索引对于索引,site.com/News/123对于个别新闻。
-
Then my 新闻管理员是这样的:
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Examine;
using Umbraco.Core.Models;
using Umbraco.Web;
using Umbraco.Web.Models;
using Umbraco.Web.Mvc;
namespace mebli.Controllers
{
public class NewsController : PluginController
{
public NewsController()
: this(UmbracoContext.Current)
{
}
public NewsController(UmbracoContext umbracoContext)
: base(umbracoContext)
{
}
public ActionResult Index(string id)
{
var criteria = ExamineManager.Instance.DefaultSearchProvider.CreateSearchCriteria("content");
var filterNews = id == "0" ? criteria.NodeTypeAlias("News") : criteria.NodeTypeAlias("News").And().NodeName(id);
var resultNews = Umbraco.TypedSearch(filterNews.Compile()).ToArray();
if (!resultNews.Any())
{
throw new HttpException(404, "No product");
}
if (id == "0")
{
criteria = ExamineManager.Instance.DefaultSearchProvider.CreateSearchCriteria("content");
var filterNewsRepository = criteria.NodeTypeAlias("NewsRepository");
var newsRepository = Umbraco.TypedSearch(filterNewsRepository.Compile());
var renderModel = CreateRenderModel(newsRepository.First());
return View("NewsIndex", renderModel);
}
else
{
var renderModel = CreateRenderModel(resultNews.First());
return View("News", renderModel);
}
}
private RenderModel CreateRenderModel(IPublishedContent content)
{
var model = new RenderModel(content, CultureInfo.CurrentUICulture);
//add an umbraco data token so the umbraco view engine executes
RouteData.DataTokens["umbraco"] = model;
return model;
}
}
}
它继承自 Umbraco插件控制器,别问为什么:)
-
第三,我有两个从控制器调用的视图 -新闻索引对于索引和News对于个别新闻。例如,我的 NewsIndex.cshtml 在这里:
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@{
Layout = "~/Views/Page.cshtml";
}
<div id="main-content" class="body news narrow">
<h2>@Model.Content.GetPropertyValue("header")</h2>
<ul>
@foreach (IPublishedContent news in Model.Content.Children.OrderBy("date desc"))
{
<li>
<span>@Helpers.FormatDate(news.GetPropertyValue("date"))</span>
<div>
<a href="@Url.Action("Index", "News", new { id = news.Name })">@Helpers.StripHtml(news.GetPropertyValue("Brief").ToString())</a>
</div>
</li>
}
</ul>
<div class="clr"></div>
</div>
其实我无法解释这段代码中的每一行,因为我不久前开始学习 ASP.Net MVC 和 Umbraco。但我认为这个想法很明确。它有效:)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)