So I know that I am able to obtain a content node with the following code:
Node contentNode = new Node(contentId);
I would like to access a content blueprint from within my code in a similar fashion, how would I go about this?
I clearly didn't understand what a content template was, so thank you Claus for explaining this to me.
I did eventually find the solution to my problem. It turns out that content templates are also referred to as blueprints. You can obtain the content template (AKA blueprint) with the following code:
ApplicationContext applicationContext = ApplicationContext.Current;
// Obtain the Umbraco Content Service
var contentService = applicationContext.Services.ContentService;
// Obtain content template
var contentTemplate = contentService.GetBlueprintById(contentTemplateId);
If you then wanted to create a content node from this blueprint you could use:
// Create content node from content template
var content = contentService.CreateContentFromBlueprint(contentTemplate, "Content Node Name");
// Save and publish to new content node
Attempt<Umbraco.Core.Publishing.PublishStatus> publishStatus = contentService.SaveAndPublishWithStatus(content);
Content templates are simply .cshtml view files on disk. You can get the path of the view file for a template, using the FileService in the Umbraco API (it has methods for templates, partial views, scripts & stylesheets...). The Name property should have the filename including extension.
Then it's just a matter of editing the file if that is what you wanted to do.
The code editors in the backoffice are basically just getting and saving files from disk using the CodeFileController so you can have a look at that for inspiration if you have to do something similar.
Related
I currently have my solution set up to produce Swagger documentation for each end point. However I have several end points that are only available for admins. Down below you will be able to see an example.
A regular user can create models, however only an admin can pull every single model in the database.
The challenge is to generate 2 sets of swagger documentation? One for regular users to see, and another piece of documentation for Admin users to see. I know that if I add [ApiExplorerSettings(IgnoreApi = true)] to my end point it will not appear in the documentation generated however this would mean that my admin users wont be able to see that vital piece of documentation as well. Any recommendation on how to dynamically generate two sets of documents depending on the user will help.
[SwaggerResponse((int)System.Net.HttpStatusCode.OK, Type = typeof(RestOkResponse<PackageResponse>))]
[SwaggerResponse((int)System.Net.HttpStatusCode.InternalServerError, Type = typeof(RestErrorResponse))]
[SwaggerResponse((int)System.Net.HttpStatusCode.BadRequest, Type = typeof(RestErrorResponse))]
[SwaggerResponse((int)System.Net.HttpStatusCode.Forbidden, Type = typeof(RestErrorResponse))]
[SwaggerResponse((int)System.Net.HttpStatusCode.NotFound)]
[HttpPost("/v1/packages")]
[Authorize()]
public async Task<IActionResult> CreateModel([FromBody]Request request)
{
...
}
The method below is for admins only:
[SwaggerResponse((int)System.Net.HttpStatusCode.OK, Type = typeof(RestOkResponse<PackageResponse>))]
[SwaggerResponse((int)System.Net.HttpStatusCode.InternalServerError, Type = typeof(RestErrorResponse))]
[SwaggerResponse((int)System.Net.HttpStatusCode.BadRequest, Type = typeof(RestErrorResponse))]
[SwaggerResponse((int)System.Net.HttpStatusCode.Forbidden, Type = typeof(RestErrorResponse))]
[SwaggerResponse((int)System.Net.HttpStatusCode.NotFound)]
[ApiExplorerSettings(IgnoreApi = true)]
[HttpPost("/v1/packages")]
[Authorize()]
public async Task<IActionResult> GetAllModelsFromDatabase([FromBody]Request request)
{
...
}
A dynamic process was found in this answer.
Dynamically Ignore WebAPI method on controller for api explorer documentation
It is possible to separate swagger documents however there is no built in method to do this. One would have to remove the un wanted nodes from the one documentation file:
https://github.com/swagger-api/swagger-editor/issues/233
This works fine in current editor if you host the editor yourself so
parameters_common.yaml path can get resolved as an HTTP path.
Currently there is no way to jump between files or create a new one.
If you are doing a big project with Swagger, I recommend hosting the
editor yourself. When editor and the API you are building are on the
same origin, XHR call don't have to be cross-origin which help editor
to show more details about calls in "try-operation" and your API
doesn't have to have cross origin headers.
Example on how to split swagger file into smaller nodes.
http://azimi.me/2015/07/16/split-swagger-into-smaller-files.html
I want get a content of html file using C# and the pass those into cshtml view page. is this possible to do ?
my main requirement is load a html file into TinyMCE editor content, which is located at another destination.
but once I explore about this I saw given answers like below
You cannot give a path to the setContent function directly. What you need to do is to get the html file contents backendside and send it to your page where you insert it into your editor using the setContent method.
but I dont know how to get the html file content and send it to page in asp.net mvc
However I tried like below to insert html file using jquery
setup: function(ed) {
ed.on("init", function(ed) {
$.get("myhtml.html", function (content) {
tinyMCE.activeEditor.setContent(content);
});
})}
but this once also not working for me. any suggestion would be highly appreciate
This is very simple. Instead of using it from the jQuery i'll suggest to create the MVC controller action with string return type and then use File.ReadAllText method of System.IO assembly to get the content of the html file.
public class HomeController : Controller {
public IHtmlString TyneMice()
{
return new MvcHtmlString(System.IO.File.ReadAllText("filename"));
}
}
you can call this controller and action from jQuery.
I hope this answer helps you. If you need more detail on how to read the content from html file you can find here
We are developing an e-commerce system that multiple affiliate partners will use. We would like to tailor the portal for each partner and be able to accommodate slight variations in content from page to page. Our current technique has been to create a copy of a .cshtml view for each partner and make the customization to each view. Our designer is groaning because may of these views only have slight variations in wording. We only plan to have 10 or so partners (it cannot expand beyond that because of the size of our industry) so a full blown CMS system is overkill.
I would like to use resx files manage content strings for each partner the way one would use them to manage content strings for different languages. The end result would be the ability to do something like this in a view.
Please contact customer service at #Properties.Resources.PartnerCustomerServiceEmail
at not have to worry about which resource file is used to resolve the string PartnerCustomerServiceEmail.
Thank you in advance for your help
First idea that comes to my mind is to save resource file's name in question into viewdata (or Session) and use a helper to get the value.
Say you have two partners: Foo Logistics and Bar Solutions. Have a resource file for each of them: PartnerFoo.resx and PartnerBar.resx.
In your controller, store the resource file you want to use into ViewData as in:
public ActionResult About()
{
...
ViewData["Resource"] = "MyMVCAppNamespace.Resources.PartnerFoo";
return View();
}
Include the namespace into the string too.
Then code in the helper to retrieve the resource with viewdata.
Helpers/Helper.cs:
namespace MyMVCAppNamespace.MvcHtmlHelpers
{
public static class HtmlHelpersExtensions
{
public static ResourceManager PartnerResource(this HtmlHelper helper)
{
// Get resource filename from viewdata
string res = helper.ViewContext.ViewData["Resource"].ToString();
// Load the resource from assembly
ResourceManager resourceManager = new ResourceManager(res, Assembly.GetExecutingAssembly());
return resourceManager;
}
}
}
Now in the view, we are gonna use this helper to retrieve the string we want to write:
About.cshtml:
#Html.PartnerResource().GetString("PartnerName")
#Html.PartnerResource().GetString("PartnerCustomerServiceEmail")
Which gets rendered as:
Foo Logistics service#foologistics.com
or with PartnerBar
Bar Solutions service#barsolutions.com
We determine the resource file to use before the view is loaded. Then in view it gets dynamically rendered according to what resource is stored in to the viewdata. You can even store the resource filename into web.config and load the string in helper from there if you want.
What's even more cool is that if you have localized resx file, say PartnerFoo.fi.resx and then use different culture (fi-FI in this case), the resourcemanager automatically looks up the localized version.
You can even do simple branding by storing image URLs and whatnot in the resource file.
It's simple really, but I hope it gets you started.
I'm using MVC.I want to save xml file inside the services class.
so wrote this one.
string path = HttpContext.Current.Server.MapPath((Url.Content("~/client-authentication.xml")));
but there is error and it says
'System.Security.Policy.Url' does not contain a definition for 'Content'
How to solve it??
How can I give the path??
Is this code in your view(.cshtml) or controller(.cs)?
if cshtml, you can write "string path = Url.Content(...)" directly, no need Server.MapPath.
if controller, just Server.MapPath(...), no need Url.Content.
PS, you can also use AppDomain.CurrentDomain.BaseDirectory to retrive physical path of your site root.
This is the wrong Url class. You want the System.MVC.Web.UrlHelper instance called Url which is a property provided on MVC controllers.
I have an ASMX service which serves some partial-HTML to a web-app as part of a JSON object. Until now, I've just been building the HTML in code, using StringBuilders. This is a huge pain since the formatting is really hard to read and I can't use any of Visual Studio's/Resharper's code completion, syntax highlighting, and other convenient features.
I tried to solve this with User Controls (I'm not committed to this approach if there is a better way. All I need are some very simple parametrized static partial HTML pages), but now I am running into problems when I try to render the control like this:
public override string Html
{
get
{
StringWriter writer = new StringWriter();
HtmlTextWriter htmlWriter = new HtmlTextWriter(writer);
CreateTestWizardPartials.TestPeriods testPeriodsHtml = new CreateTestWizardPartials.TestPeriods();
testPeriodsHtml.RenderControl(htmlWriter);
htmlWriter.Flush();
return writer.ToString();
}
}
This always returns an empty string. I've read you need to use Page.LoadControl() to dynamically load User Controls but there is no Page for me to use it with. Is there a workaround or a better solution than User Controls?
Thanks!
You could load static HTML file that contain the partial bits of markup you need. If you need to somehow bind the markup with dynamic data, then render the HTML files through a template engine.
Here's and older post regarding some template systems for ASP.Net:
Can you recommend a .net template engine?
And another more recent post regarding ASP.Net MVC:
JQuery's $ is in conflict with that of StringTemplate.Net in ASP.Net MVC