Shared layout priority - c#

I have _layout1.cshtml in Areas/Book/Views/Shared/_layout1.cshtml and in /Views/Shared/_layout.cshtml.
Areas/Book/Views/Home/Index.cshmtl:
#{
Layout = "~/Areas/Book/Views/Shared/_layout1.cshtml";
}
Somehow it stills show the content of _layout1 in Root /Views/Shared instead of in the Areas/Book
Is there any priority when the layout view share the same name?

The code as you posted it here should work. I am using the same successfully here.
Make shure the path is correct and the layout file is where you say it is.
Maybe odd, but what could cause this if the file is there:
If your APP is running with a differen user which does not have access rights to the file it could happen that the file "is not there" for the app.

Related

unable to load a local resource OR virtual path error

Situation
I have two applications that both read and save images / documents to one specific folder. Example if the user uploads their image in one program, a user of program B is supposed to view and even edit that image.
What I have done
In one of my application I have created a setting in settings for the string of my path. So to save an image path I call
path = System.IO.Path.Combine(Properties.Settings.Default.imagePath, Fimage);
this works fine.
However, my issue is when I try to view the image in my edit view.
Controller
ViewBag.imagePath = Properties.Settings.Default.imagePath;
View
<img src="#ViewBag.imagePath/#Url.Content(#Model.image)" alt="Image" style="height: 255px; " />
Problem
The problem is upon attempting to view the image I get the error, Not allowed to load local resource: I have full access to the folder and when I attempt to browse to the file in the error message the picture is displayed. I was advised from other questions to use, server.MapPath however when I do Server.MapPath(Properties.Settings.Default.imagePath); I get the error physical path received, expected virtual path.
This viewbag holds the entire string of folder my files are stored with the EVERYONE user having full access. So i'm really unsure of why it's making a fuse.
P.S I cant say something like "~/content/images" because the path to that file is in an entirely different application, I think I need to give it the entire location.
Any assistance will be appreciated.
Gratitude
Looks like your defaultImage variable holds a physical path, which will work for saving the image since the API requires a path in the form e.g. c:\website\images\.... but for viewing, you need the VirtualPath, something of the form http://mycoolsite/images/image.jpg and this is what the error is telling you.
Your Controller; therefore, should return something like
ViewBag.imagePath = Properties.Settings.Default.imagePath.Replace(Request.ServerVariables["APPL_PHYSICAL_PATH"], String.Empty);
In order to get the Relative Path. See this other related question.
EDIT I just saw your "PS..."
If that's the case, then you need to pass the full URL to the other application: (e.g. http://someothersite.com/images/image.jpg).

C# Windows Forms - How to programmatically set image preview for file folder (VSTO 2010)

Windows Forms - VSTO - Outlook
Background - I am creating a digital archive add-in for Office where the user can search the database for the client (whom the document belongs to) and it will save the file to the appropriate folder(s) based on the nature of the file. So far this is working for Word as planned but I am now using Outlook which has more to consider (attachments, message body, etc.).
I have got it working so far that the attachments are saved into a temporary folder (which is emptied each time the windows form closes) ready to be sorted and I can obtain information about sender/subject/email body. The list of attachments is set out into a CheckedListBox
Current Problem - When a user is looking to archive an attachment (a lot of documents/scanned documents will come up), images will be confusing as they may be necessary or entirely unimportant so I wish to preview the images.
I am trying to get it so on the event of
private void chkAttachments_SelectedValueChanged(object sender, EventArgs e)
The image shows in picAttachPreview (PictureBox) as a preview of that file. This will be taking the image from tempfolder (#"c:\temp\DigitalArchive").
I understand this is wrong but I am trying to set the source for the image shown on screen on that SelectedValueChanged event.
My [Incorrect] Code -
if(chkAttachments.Text.Contains(".jpg"))
{
var selectedImage = chkAttachments.SelectedValue.ToString();
picAttachPreview.Image = tempfolder + #"\" + selectedImage; //(A)
}
The (A) line is the issue and although I understand why, I don't know how to resolve it. The filepath is constructed with tempfolder and selectedImage (e.g. ScannedDoc.jpg) but the file path type is String but picAttachPreview is System.Drawing.Image so I assume I am looking at the wrong property of picAttachPreview to set the source of the image.
Any help or guidance will be immensely appreciated. Thank you.
(Also if you know of any good way I can set the same nature of preview for documents/PDF then I will be immensely grateful)
Edit Although the link solves part of my problem, there is an issue with chkAttachments.SelectedValue.ToString() which I answered below. (If anyone can advise me on the site etiquette for this situation. Do I delete the question or leave it with the answer I found so that people can find the solution to the same problem in future? Thank you)
After some playing around, I found another problem (chkAttachment.Text works whereas .SelectedValue.ToString() does not.
Also the issue with the string to image format is resolved by prefixing the path with Image.FromFile(
So the correct way of changing the image upon selection is:
if(chkAttachments.Text.Contains(".jpg"))
{
var selectedImage = chkAttachments.Text;
picAttachPreview.Image = Image.FromFile(tempfolder + #"\" + selectedImage);
}

Use of ServerMapPath in C#

In a block of code, i have the following line:
CsvFile= #"D:\Web\Preps\en\csr\downloadcenter\ClickCounter.csv";
I try to use Server.MapPath instead:
CsvFile = Server.MapPath(#"../en/csr/downloadcenter/ClickCounter.csv");
(the file from where i write this line is located at the same level as "csr" but in a different folder)
I don't have any errors appearing since i'm not using visual studio. Does anyone know what i'm doing wrong ? Thanks for your help
You said that your file is in a folder at the same level of the csr folder, something like
D:\Web\Preps\en\YourFolder, am I right ? Then, your path is not correct.
Try this :
CsvFile = Server.MapPath(#"../csr/downloadcenter/ClickCounter.csv");
or this :
CsvFile = Server.MapPath(#"../../en/csr/downloadcenter/ClickCounter.csv");
The reason your path is not correct is because the way you're using it, you're trying to access
D:\Web\Preps\en\en\csr\downloadcenter\ClickCounter.csv. There is an unneeded en
Assuming that the directory you are after is "under" your web site directory then:
Server.MapPath("~/")
Will take you to the root of your website, from there navigate to somewhere else i.e.
Server.MapPath("~/en/csr/downloadcenter/ClickCounter.csv")
If you don't want to get to the root of your site and it's located somewhere else on the server, then I have misunderstood the question. In this instance you will need to ensure the site has permissions to the respective directory.

Htmlfile access is denied when using Server.MapPath

I have been using Server.MapPath("page.aspx") for quite a long time, but it is just now that I faced this problem.
Basically here is my code
Session.Clear();
ShowLoggedOffControl(); //A function that setup bunch of controls visibility
OnUserLoggedOut(new EventArgs());
Response.Redirect(Server.MapPath("~/Default.aspx"));
The error would be htmlfile:access is denied at javascript execution. However when I removed Server.MapPath so that it became like this Response.Redirect("~/Default.aspx");, things work normally.
What did I do wrong? Why, how and when can I use Server.MapPath?
Thanks.
Server.MapPath maps the specified relative or virtual path to the corresponding physical directory on the server. So in your example it would end up redirecting to something like this:
c:\Projects\MyWebsite\Default.aspx
which is probably not what you want.
Response.Redirect on the other hand will resolve the '~' to the relative path root for you and resolve to something like this:
/MyVirtualDirectory/Default.aspx
As for when you would want to use Server.MapPath, you would use it if you wanted to actually find the file on the server and do something such as:
var lines = System.IO.File.ReadAllLines(Server.MapPath("~/MyTextFile.txt"));
// Do something here with values found
Server.MapPath gets the physical path to the file on the hard disk, whereas Response.Redirect expects a URL.
If for some reason you need to get the full URL, you can use this:
String.Format("http://{0}{1}", Request.Url.Host, Page.ResolveUrl(relativeUrl));

Open File In Silverlight 4 File operation not permitted

I have a Silverlight web application(4.0) with a select file open dialog, however I get this error when the user selects a file : "File operation not permitted Access to path '' is denied"
When I try to debug it then I get this security exception "Dialogs must be user-initiated."
Is there a way around this? Has anyone has tried doing this in Silverlight?
Here is my code so far which hasn't worked:
OpenFileDialog dlg = new OpenFileDialog
{
Multiselect = false,
Filter = "All files|*.*"
};
bool? userClickedOK = dlg.ShowDialog();
if (userClickedOK == true)
{
textBox1.Text = dlg.File.FullName;
}
Because of security related restrictions you cannot open file dialogs in Silverlight directly. You can only open dialogs from inside an event handler like mouse click.
In silverlight 4, you cannot acces the FullName property, this is the cause of exception: "File operation not permitted Access to path is denied"
I trien utmost but cannot find a way to get full file path of selected file without making your application OOB.
While debugging a silverlight project, if you place a break point anywhere before the dlg.ShowDialog(), in case of your code this will raise the exception: "Dialogs must be user-initiated"
Simple way to avoid this exception is to place your break point after ShowDialog() line.
As far as I know you are not allowed to access user files if you don't have elevated permissions.
You can't get the full name of a file. And in all cases, why would you need it? There is no reason to know where the user stores her files.
If you want to read a file, use Stream property of the uploaded file instead.
I was having the same issue, after reading a lot about this problem that you cannot access dlg.File.FullName
Instead you can use this
dlg.File.Name
by doing this your exception will be removed
The error also occurs if you try to access CreationTime from a FileInfo.

Categories