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.
Related
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.
I am getting error: object reference not set to an instance of an object in this line of code in my Controller class method:
var ChangeEmailUrl = Url.Action("ChangeEmailConfirmation", "ManageAccount");
My objective is to get a URL path to an action 'ChangeEmailConfirmation' in controller 'ManageAccount'. The string link returned by the Url.Action() method would be sent in an email.
Also, I would like to know if there is any other way to get URL link. Any light on solving this problem would be appreciated.
If you wanted to get an absolute url
(http://localhost:8385/MyController/DoThis):
var ChangeEmailUrl = Url.Action("DoThis", "MyController",null,Request.Url.Scheme,null)
See also
you can try with urlhelper or htmlhelper
Url Helper:
Contains methods to build URLs for ASP.NET MVC within an application.
Official doc:https://msdn.microsoft.com/en-us/library/system.web.mvc.urlhelper(v=vs.118).aspx
HtmlHelper.GenerateLink Method:
Generates an HTML anchor element (a element) that links to an action method
https://msdn.microsoft.com/en-us/library/system.web.mvc.htmlhelper.generatelink(v=vs.118).aspx
Example:
https://stackoverflow.com/a/700357/3397630
Hope it was useful
Thanks
Karthik
I'm attempting to make a Virtual directory relative link to some unknown (to WebAPI) resource form an ApiController.
Example:
http://some/path/webapi/does/not/know
The WebAPI Url helper seems to be tightly coupled with Routing and does not have a Content() method like the MVC variant. I'm trying to avoid using any non-mockable HTTP context information to make this call (such as HttpContext.Current).
Thanks for the help!
You can always do,
var urlBuilder =
new System.UriBuilder(Request.Url.AbsoluteUri)
{
Path = "webapi/does/not/know"
};
var uri = urlBuilder.Uri
In that way, you don't need to rely on the UrlHelper. The base url is inferred from the current request.
Here are two options you can try:
HostingEnvironment.MapPath()
Create a System.Web.Mvc.UrlHelper in your API controller
In Controller it's easy to access the virtual path you need to access like:
Server.MapPath(#"~\App_Data\blah\blah")
This give you access to AppData folder, but if I want to access them in Models, how can I acccess the virtual path in MVC 3?
How can I access my app_data folder in Models of my application ?
If i were you, rather than figuring out how to access the current execution path, I wouldn't break my App layers and pass it as an argument to my model
Your model should not access it - get the controller to provide the data needed.
As Aliostad said, you should have the Controller access it, the model should only hold model data. So here are 2 ways to use the controller to access it.
If virtual folder is in the root of the web application. (If you have to drill down further, just add more parameters to the path combine until you get to your folders location.)
string folderPath = Server.MapPath(System.IO.Path.Combine(Request.ApplicationPath, "VirtualFolderName"));
For a more re-usable solution i created a Extension for the Controller class:
using System.IO;
using System.Web.Mvc;
namespace Extensions {
public static class ControllerExtensions {
public static string ResolveVirtualFolderPath(this Controller controller, string folder_name) => controller?.HttpContext?.Server?.MapPath(Path.Combine(controller?.HttpContext?.Request?.ApplicationPath, folder_name));
}
}
Then in the Controller put the using statement so you can access the extension
using static Extensions.ControllerExtensions;
then you can do this in the controller:
string folderPath = this.ResolveVirtualFolderPath("VirtualFolderNameHere");
You could break the extension down to not use the null check operator "?" and do if null and then handle each situation like the folder does not exist, or maybe access to location is not allowed or whatever else your needs may be.
If I have a url like "/something/something/" and my site is http://mysite.com and I want to link to that something url, is there a method like Url.Content(); which will discover the virtual directory of the site in IIS and map appropriately to a url path?
I tried Url.GenerateContentUrl(), Url.Action(), Url.Content(), Url.RouteUrl()
is there a method like Url.Content();
Why like when there is Url.Content?
var url = Url.Content("~/something/something");
which will take care of the virtual directory name and if your side is deployed at the root http://example.com it will return /something/something and if you have a virtual directory http://example.com/applicationname it will return /applicationname/something/something.
So everytime you need to link a static resource you should always use Url.Content. For example:
<img src="<%= Url.Content("~/images/foo.png") %>" alt="" />
will always correctly resolve the image url no matter where your site is deployed to.
If, for whatever reason, you can't just use Url.Content, you can use either HostingEnvironment.ApplicationVirtualPath or HttpContext.Current.Server.MapPath instead.
But I can't think of a good reason you can't just use Url.Content.
This is what I assume:
You have a file like myfile.jpg on the root on your site. Then you want a url like:
http://mysite.com/myfile.jpg
Right?
All you need to do is add this in yours routes in Global.asax.cs:
routes.IgnoreRoute("myfile.jpg");
Yes it should work on sub folders too.