ASP MVC 5 - Partial View File Outside of Views Folder - asp.net

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

Related

Organizing ASP.NET Web Application Project

How do I update references to user controls after putting ascx and aspx files into different folders?
I'm not used to working with ASP.NET Web Forms projects, but I inherited this one. When I first received this Project, every file was directly under the root; no folders whatsoever. I've started putting files into folders and updating the paths.
When I try to load a user control, however, I get this error:
Cannot use a leading .. to exit above the top directory.
My user control is located at:
myProject\controls\Widget.asxc
My page is located at:
myProject\Pages\Activity\AST_Page.aspx
Markup from AST_Page.aspx:
<%# Register TagPrefix="tf" TagName="Cost" Src="..\..\controls\Widget.ascx" %>
My understanding is:
When a user navigates to AST_Page.aspx, the current path is myProject\Pages\Activity\.
Starting a path with ..\..\ should put me at myProject\.
Therefore, ..\..\controls\Widget.ascx actually points to myProject\controls\Widget.asxc.
My Project has access to my myProject\.
If I change ..\..\controls\Widget.ascx to ..\controls\Widget.ascx, I get a different error about not finding the file, which is expected as Widget.ascx is not under myProject\Pages\controls\.
There was a second issue on the page. I had "~/../../" in a src attribute; likely due to an aggressive find and replace.

Resource not exist in current context in razor view

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.

Custom Folder Structure in ASP.NET MVC 5

I'm trying to determine if it is possible (or practical) to implement Uncle Bob's Screaming Architecture in ASP.NET MVC 5 rather than using the default folder structure.
Here's a link to a description of the Screaming Architecture: http://blog.8thlight.com/uncle-bob/2011/09/30/Screaming-Architecture.html
A hypothetical folder structure would look something like this:
Root
Customers
Controllers
CustomerController.cs
Models
Customer.cs
Views
Index.cshtml
Details.cshtml
Update.cshtml
Employees
Controllers
EmployeesController.cs
Models
Employee.cs
Views
Index.cshtml
Details.cshtml
Update.cshtml
Shared
Views
_Layout.cshtml
Error.cshtml
_ViewStart.cshtml
Web.config
The corresponding URL routes would look like this:
http://www.example.com/customers/ => Customer Index
http://www.example.com/customers/details/1 => Customer Details
http://www.example.com/customers/update/1 => Customer Update
http://www.example.com/employees/ => Employee Index
http://www.example.com/employees/details/1 => Employee Details
http://www.example.com/employees/update/1 => Employee Update
I've created a custom RazorViewEngine and added the appropriate view location formats (e.g. "~/{1}/Views/{0}.cshtml") and partial view location formats (e.g. "~/Shared/Views/{0}.cshtml"). I've also moved the shared _ViewStart.cshtml to the root and merged the Views/Shared folder's web.config with the root-level web.config to avoid having to duplicate these two files in all of the View folders.
Everything works great, however, if I try navigating to an index page (e.g. http://www.example.com/employees/) I get a 403.14 Error (Forbidden). All other routes (including http://www.example.com/employees/index) work just fine.
My guess is that IIS is explicitly blocking the route to the controller's index method because the URL coincides with a folder in the filesystem and directory browsing is disabled by default. If I enable directory browsing, however, it actually takes me to the actual directory listing rather than routing to the controller's index method.
I can move the Customers and Employees folders into a subfolder (i.e. move them out of the root) and everything works fine, but I'd like to try to keep these folders at the top level (per the Screaming Architecture guidelines).
Does anyone have a solution for this issue?
Please note that MVC Areas is not the solution I'm looking for as it does not allow for the folder structure described above (i.e. top-level folders named after high-level use cases and views contained directly within the Views folder rather than in a subfolder).
I'm betting you are right about IIS then. If you have two paths mapped to the same resource, the physical path is checked first on the IIS side.
I was digging around the routes configuration and found the property RouteExistingFiles on RouteCollection and think this could work.
I set the value to true and tested locally with an empty folder in the project, a route redirecting to Home/Index, and navigating to localhost:xxx/MyFolder. It worked correctly.
So then all you should need to do is set this property to true for it to choose Asp.net routes first instead of physical routes.

Asp.net mvc3 localization - linking resource with view?

I use the localization paradigm of one resx file per view in all my projects. I have two identical folder structures for Resources and Views and I link the resource and the view like so:
#using Res = [Namespace].Resources.[Controller].[View]
<p>#Res.[ResourceKey]</p>
Is it possible to get asp.net to use/link the resource file without the using statements and defining a Custom Tool Namespace for all my Resx files? (Yes, I am that lazy.)
Open your web.config in your Views folder.
Add the namespace for your resources.
Restart VS as it doesn't pick up the namespace so it's not showing intellisense.
Call your resources from any view without adding using statements inside your *.cshtml files.

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