User Tools

Site Tools


orchard_snippets

This is an old revision of the document!


Orchard Snippets

Create a route

public class Routes : IRouteProvider {
	private const string ModuleName = "Microsoft.RoadmapPOC";
 
	public IEnumerable<RouteDescriptor> GetRoutes() {
		return new[] {
			new RouteDescriptor {
				Priority = 9999, // This route is pretty bloody important.
				Route = new Route(
					"Roadmap/{action}",
					new RouteValueDictionary {
						{"area", ModuleName},
						{"controller", "Roadmap"}
					},
					new RouteValueDictionary(),
					new RouteValueDictionary {
						{"area", ModuleName}
					},
					new MvcRouteHandler()
				)
			}
		};
	}
 
	public void GetRoutes(ICollection<RouteDescriptor> routes) 	{
		foreach (var route in GetRoutes()) 		{
			routes.Add(route);
		}
	}
}

Controller

public class RoadmapController : Controller {
	public ActionResult Test() {
		return Content("Hello there!");
	}
 
	[HttpPost]
	public ActionResult Upload() {
		byte[] bytes = new byte[Request.InputStream.Length];
		Request.InputStream.Read(bytes, 0, (int)Request.InputStream.Length);
		var jsonPayload = Request.ContentEncoding.GetString(bytes);
		...
	}
}

Part extraction in a view

// Statically typed.
var contentItem = (ContentItem)Model.ContentItem;
var titlePart = contentItem.As<TitlePart>();
var animalName = titlePart.Title;
var speciesField = (TaxonomyField)contentItem.Parts.SelectMany(x => x.Fields).Single(x => x.Name == "Species");
var species = String.Join(" ", speciesField.Terms.Select(x => Html.ItemDisplayLink(x)));
var genderField = (BooleanField)contentItem.Parts.SelectMany(x => x.Fields).Single(x => x.Name == "Gender");
var genderDescription = genderField.Value == true ? T("male") : T("female");
 
// Dynamically typed.
var contentItem = Model.ContentItem;
var titlePart = contentItem.TitlePart;
var animalName = titlePart.Title;
var speciesField = (TaxonomyField)contentItem.Animal.Species;
var genderField = contentItem.Animal.Gender;
var genderDescription = genderField.Value == true ? T("male") : T("female");
var species = String.Join(" ", speciesField.Terms.Select(x => Html.ItemDisplayLink(x)));
orchard_snippets.1424915963.txt.gz · Last modified: 2017/01/01 19:50 (external edit)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki