一把umbraco 7更新一把umbraco路线 [英] Umbraco 7 Update Umbraco Routes
问题描述
我想创建一把umbraco 7 MVC应用程序。这样做,我希望能够创建一个管理后台数据的自定义控制器。通过我的研究,我发现使用SurfaceController是最成功的。然而,路由时增加/一把umbraco /面/页面。例如,我的测试控制器和视图看起来像/一把umbraco /面/测试。是否有管理这些路线和有它只需进入/测试无添加的一把umbraco路由它的方法吗?如何在一把umbraco 7创建自定义控制器的任何指导,将是有益的!
I am trying to create an Umbraco 7 MVC application. In doing so, I want to be able to create custom controllers that manage data behind the scenes. Through my research, I found using the SurfaceController was the most successful. However, the route adds "/umbraco/surface/" to the page. For example, my Test Controller and View would look like "/umbraco/surface/Test". Is there a way to manage these routes and have it simply go to "/Test" without adding the Umbraco route to it? Any guidance on how to create custom controllers within Umbraco 7 would be helpful!
推荐答案
这是我在我的项目已经实现。挖INET我找到了解决办法:
this is what I have achieved in my project. Digging inet I found the solution:
-
在 APP_ code 文件夹中,我创建文件的 Startup.cs 有路线:
In App_Code folder I created file Startup.cs with a routes:
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/News site.com/News/Index 作为指数网站.COM /新闻/ 123 个人消息。
This allows you to have routes like you want, in my case site.com/News, site.com/News/Index for index, site.com/News/123 for individual news.
然后我的 NewsController 是这样的:
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的 PluginController ,不要问为什么:)
和第三我有两个观点正在从控制器叫做 - 新闻索引作为指数和新闻作为单独的消息。例如,我的NewsIndex.cshtml是在这里:
And thirdly I have two views being called from the controller - NewsIndex for the index and News for individual news. For example, my NewsIndex.cshtml is here:
@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>
其实,我不能在这个code解释每一行因为不久前已开始学习ASP.Net MVC和一把umbraco。但这个想法是清楚的,我想。和它的作品:)
Actually I can't explain every line in this code because have started learning ASP.Net MVC and Umbraco not long ago. But the idea is clear I think. And it works :)
这篇关于一把umbraco 7更新一把umbraco路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!