Resource not exist in current context in razor view - asp.net

I have Resources.resx file created in my webapp wwwroot/Resources folder.
When i try to use it in razor view as #Resources.SomeValue i got an error
The name 'Resources' does not exist in the current context
When i move file to wwwroot folder, i can use resource.
I have tried to use #Resources/Resources.SomeValue, but i got same error
Is there a way to move this file somewhere away from root folder?

In the web.config file in your Views folder, add a namespace to the namespaces node for your Resource folder. Should look something like this:
<add namespace="wwwroot.Resources" />
This should allow you access to that folder in your views. You may have to close and reopen the view to get Intellisense.

Related

ASP MVC 5 - Partial View File Outside of Views Folder

I have a partial view file which receives a #model saved outside of the views directory - actually stored in app_data.
However I get error:
The name 'model' does not exist in the current context
Source File: ...\app_data\_DefaultLayout.cshtml
Below is my code:
#Html.Partial("~/app_data/_DefaultLayout.cshtml"), Model)
Default Layout is as follows:
#using System.Web.Mvc
#using System.Web.Mvc.Html
#model MyApp.ViewModels.CreateCaseViewModel
// some html and razor code
This exception is a compilation error on the cshtml file. The view is not having access to model type. If you check your Solution Explorer, there are two web.config files, one in the Views folder and one at root level.
One way to sort it is making a copy of Views/web.config into App_Data.
App_Data is a special directory in ASP.NET, which is supposed to store data files like XML files / mdf files to store data. It has restricted access as well.
You should not be putting any UI layer concerns( views) in that folder. Asp.NET MVC has a convention for your UI layers, which is ~/Views directory. So use that location to keep your views.
If absolutely needed, you can put your view files pretty much anywhere in the application (Except those system directories), For example, you can create a directory called MyPartialViews and put your partial view there and refer the full path to that. The important thing to remember is, you need to have a web config in that folder (copy the one from the ~/Views and put it there). The web config has many needed elements in that including pageBaseType for all views

Restrict browsing an XML file using web.config

I have an application where I have an XML file which holds the connection string.
When hit the URL with the exact file name it opens the file in the browser that is natural.
Now I need to restrict this file browsing and I can not change that code to do any redirect or anything else.
What I have tried is using denyurlsequences in the security tag in the web.config but it restricts the application too to access the file and that makes the application stop working.
I am using .NET Framework 2.0
The ".xml" file extension is not supported by ASP.NET pipeline that is why you cannot add it to web.config and restrict it. What you can do is to copy the xml file in the "App_Data" folder of your application.
The purpose of this folder is to hide it files from browsers/users and also is build for this type of data.
To create "App_Data" folder if it already does not exist :
Right click on your project -> Add ASP.NET Folder -> App_Data
Or just add a folder and call it App_Data
I know its old question but this answer may be helpful for others.
I did below configurations on IIS 8.5 ( make sure Request Filtering is installed on IIS.)
Open IIS and click on your application virtual directory.
In features View, Click on Request Filtering and then go to Hidden Segments tab - this tab will contain list of hidden files or folders. (like web.config, APP_data folder etc...)
Add file or folder you want to hide from the browsing by click on add hidden segment and mention file/folder name you want to hide and test it.
In our application we have a master xml file stored in an application folder, to deny browsing this xml file I had added the folder name in hidden segments and it worked.

ASP.NET App_Code in folder

I am working on a ASP.NET VB project. The host is pretty restrictive, so the App_Code folder has to be placed under the wwwroot folder.
My current folder structure (on the server) looks like this:
root
<-- Cant create folders or files here, host is restrictive.
wwwroot
App_Code
Class.VB
Styles
style.css
default.aspx
web.config
It seems to be working, if Class.vb contains errors, i get a compilation error on the website, so i know its compiling the class.
But i am unable to use the class in my other code files.
Example:
Dim emailFilter As Validation = New Validation()
I get this error in VS2010:
Type 'Validation' is not defined
How do i use the App_Code folder when its inside another folder?
Could it be an issue with namespaces? I might be more inclined to create a library for your validation class and add a reference to it instead.
I did find some similar issues to yours and they all referenced setting the Build Action for the class to 'compile'.
Classes residing in App_Code is not accessible

CS0103: The name does not exist in the current context dev to webserver issue

I have created a few classes that operate separately from the normal aspx.cs files. When I added a new class I was prompted to add these to app_data, i did. So I copied out these files and the app_data folder and it cannot be found. I tried the .cs files at the root of the website and they still could not be found.
I'm sure i'm messing up on a common mistake.
Thanks!
They need to be in the App_Code folder.

Page class outside of App_Code will not compile

I have a website that has 2 files as follows:
page.aspx
page.aspx.cs
It used to be that I could just drop new files onto the web server and IIS would automatically compile the files and I could access the page e.g.
http://www.website.com/page.aspx
... and the associated functionality in the page class contained in the .cs file would work nicely.
Now I get the error: "Could not load type namespace.classname" which refers to my page class.
Now for some strange reason I have to put all my .cs files, even page classes into the app_code folder.
All that has changed on my website is that I reorganised the structure so that instead of my pages being on the web root they are now inside http://.../newfolder/page.aspx.
For some reason all my page.aspx.cs files now have to be in app_code.
Any ideas?
Sounds like you are mixing up a Web Application Project and a Web Site.
Are you sure the files are exactly the same? Perhaps one #Page directive says CodeBehind=Page.aspx.cs and the other says CodeFile=Page.aspx.cs?
CodeBehind requires project compilation, so you cannot just drop in a new .cs file, you need to upload a new compiled DLL. CodeFile will allow dynamic compilation.
The App_Code directory is dynamically compiled (in both cases) when your app is accessed, so the Inherit directive has a valid type when you put the file there. In general, don't do this. You want the .cs file to go with the .aspx file. Use App_Code for business logic or utility classes that aren't associated with a particular page.
Finally, is this new subdirectory set up as a new app in IIS? What does the web.config file in your new directory change? Are you running the same version of ASP.NET? Check the "compilation" tag. I'm not sure what you could do there to cause this, but I'm sure you could cause some chaos.

Resources