In my asp.net application there is a menu built from an xml file. The menu uses a XmlDataSource with the DataFile = "~/App_Data/SideMenu.xml". That file has properties "Build Action"=Content and "Copy to Output Directory"="Do not copy".
Now I want to use this file in my own code, and I tried to open it with GetContentStream, but that doesn't seem to be possible in asp.net (problem with Application object). I am a bit reulctant to change the settings for the file.
What are my options? (XmlDataSource obviously does it)
The first step as you already mentioned is to set Build Action = Content for the file.
Then to read the file you can use HttpServerUtilityBase.MapPath to get the physical file path. From within an ASP.NET MVC controller you can simply use the Server property:
var xml = XDocument.Load(Server.MapPath("~/App_Data/SideMenu.xml"));
Or access it from the Server property on the global HttpContext.Current object.
Related
I'm new to ASP.NET and am working on a file parser and was wondering what the structure of my project should be. So far I have:
WebForm.aspx (where my html/css/js goes. an upload button calls the filehandler)
WebForm.aspx.cs (the code-behind. it's completely empty right now)
FileUpload.ashx.cs (one class that posts the selected file to my server)
FileParser.cs (classes that I have written/will write in C# already that will read in a file and parse it the way I like and return another file with the analytics and save to the user's computer)
I'm wondering where the FileParser.cs files should go. I need to be able to read the contents into an object that will parse the file to do some analytics and then give the user a "Save to computer" button on the next page to save the analytics file. I'm guessing I also need another handler for that?
The fileparser.cs must go to the App_Code folder, or you can create a new dll library and compile it, then place it to Bin directory of your asp.net
Then the classes inside the fileparser.cs can be accessed by the code behind.
More to read:
ASP.NET Web Project Folder Structure
Shared Code Folders in ASP.NET Web Site Projects
Codebehind and Compilation in ASP.NET 2.0
I am using WebBrowser control in my C# winform application, And in URL property I am giving Path of local html file. Below is the code of of my PrintAds.Designer.cs
this.webBrowser1.Url = new System.Uri("D:\\PrintAds.htm", System.UriKind.Absolute);
Every thing is working fine, But I want to add this html file with the project, and I want to dynamically pass the URL to WebBrowser control, How do I do that?
This project is going to be used offline so I cant pass website URL.
So you want to access the HTML file relative to the application root?
In which case can't you just pass something like:
var localURL = Path.Combine(Directory.GetCurrentDirectory(), "PrintAds.htm");
this.webBrowser1.Url = new System.Uri(localURL, System.UriKind.Absolute);
(You need to import the System.IO namespace)
The html file itself should be included within the project, in this example in the root (with a build action as Content)
Please note - Directory.GetCurrentDirectory gets the current working directory - which by default is the application root directory, but can be changed - See the answer at link for alternatives
My question is simple
if i have multiple directories with pages like -
/admin/adminlogin.aspx
/user/userlogin.aspx
/default.aspx
i want to set default page for each directory how ever i can set default doc for root in web.config but is there any way to assign separate for each dir ? i didn't find it anywhere.
You can easily do it from IIS. Go to IIS> your websit, set default document for each directories manually. it will add different config files automatically.
Otherwise if you are using single config the you can try <location> tag:
Click here fro more references!
In my project i will be having an link like
Download
I want the users to download files of different types. The file will be in the root folder. When i am clicking on the link it is displaying an error. This is the plugin to install in the chrome. If the user download this link and open then it will automatically add to the chrome.
How can i do this.
The file is not even downloading.
This isn't a valid path:
~/hello world.crx
The ~ character is for use server-side to denote the root of the application. Client-side it has no meaning. The browser doesn't know what the root of the application is (or what the application is at all), it's just sending requests to resources at addresses. And it doesn't know what to do with that address.
You'll need to either use some server-side logic to translate that path into a browser-useable path, or manually make it a relative or absolute path.
If the ASP.NET MVC Framework isn't translating this for you then you're probably using a version that requires a little more manual work for it. Try something like:
Download
(Note: This assumes the use of the Razor view engine. If you're not using that then you'll want to use whatever your view engine equivalent is.)
What you need to do is set up a directory online, where you can host the file.
I also see that in your aref you don't want to type the full path so denote it with a /hello_world.crx, but make sure that you've set up a base href:
<base href="http://yourdomain.com/something/">
Try renaming the file to remove any spaces e.g. "hello_world.crx" and then change the name in the link code to match.
if a webpage and the downloadable file is in the same location
(i.e)
SampleFolder->Download.html
SampleFolder->hello world.crx
then try the below
download
If the webpage and the downloadable file in different location
(i.e)
SampleFolder->Download.html
SampleFolder->Downloads->hello world.crx
then try the below
download
Is there any way to show user popup to give the name to create folder
And with that if he does not want to create folder then he should be shown the list of folders to which he want to save the file
Is it proper way that I create a popup with javascript with the list of folders on my server and put a textbox and button to save the new and in code we will create directory in code passed with textbox?
string pathToCreate = "~/UserFolders/" + TextBox1.Text;
if(Directory.Exists(Server.MapPath(pathToCreate))
{
//In here, start looping and modify the path to create to add a number
//until you get the value needed
}
//Now you know it is ok, create it
Directory.CreateDirectory(Server.MapPath(pathToCreate));
string targetPath = Server.MapPath("FolderName"); //with complete path
if (!System.IO.Directory.Exists(targetPath))
{
System.IO.Directory.CreateDirectory(targetPath);
fileToUpload.PostedFile.SaveAs(targetPath + ImageFileName);
}
else
{
fileToUpload.PostedFile.SaveAs(targetPath + ImageFileName);
}
As I mentioned by commenting that Functionality like SaveFileDialog is only possible in Windows Forms application, you cannot do this in asp.net
The alternatives in asp.net are difficult however as an alternate there is a third party editor available on the web called CKEditor 3.x Developer's Guide.
You can use File Browser (Uploader) for the purpose you want to achieve.
to create folder :
System.IO.Directory.CreateDirectory(newPath);//newpath is your folder creating path
If you are developing web applications (I can see you have tagged your question as asp.net), you do not need to worry about the pop up window. You just send the file through the Response and the download window will be shown by the browser.
Using the Example given in How to Create a Folder or File You can create a folder, but in asp.net you can not Use open file dialog or save file dialog functionalities, this will work only in C# Windows forms application.
Web applications and the HTTP protocol do not support direct manipulation of the server-side file system. In fact, that would be a dangerous idea.
It sounds like you have a base folder somewhere on the web server where you want users to be able to upload files, with some user control over subfolders and location of the uploaded file. I would suggest dong this by presenting the file system in a tree view or a list of links (where the links let you navigate up/down the folder tree). Combine this with a file input and you've got yourself a solution. HOWEVER, be very careful to control and cleanse the specified file name for the uploaded file. There are many combinations (such as utilizing "..") that can allow the user to hack into unwanted file locations on your server.