Ascx in different folder? - asp.net-mvc

I am working on an web application that uses themes and different master pages. Each master page is at the shared folder with their names; such as Shared\Themes\MyTheme\Site.master
and the views are at \ControllerName\ListUsers.aspx
In the controller; I call the view : return View("ListUsers",ThemeEngine.MasterPage,Model);
So far everything works fine; however when I try to call RenderPartial inside the ListUsers, I am getting usercontrol can't be found error because my user controls are at the master page folder such as \Shared\Themes\MyTheme\SingleUser.ascx
Is there a way to tell the framework to look for the user controls in a different folder than \Shared but \Shared\ThemeNAme etc...

Yep. You can specify the path to the View using the virtual root. There's nothing stopping you from passing in a full path.
return View("~/Shared/Themes/MyTheme/SingleUser.ascx");

How to change default view location scheme in ASP.NET MVC?

Related

MVC .NET Core 2.0 - _Layout static files do not load for action methods of a controller

I started a new project in VS 2017 and created a ASP.NET Core 2.0 web application (Model View Controller). Then I've changed the conent of the _Layout.cshtml to the interface I want to use (included #Renderbody etc.) and included all the static content it requires in the wwwroot and save and Ctrl+F5 and the layout shows with all the correct formatting and functionality. No problem so far:
However from this point forward none of the static content files of the _Layout shows in any other view. So for example if I visit any of the following URLS:
http://localhost:52786/home/
http://localhost:52786/home/index
http://localhost:52786/home/about
http://localhost:52786/home/contact
I see this:
Startup.cs already has app.UseStaticFiles() in Configure()
So it sounds like you may have your HTML body content in the wrong place. With ASP.NET MVC &ASP.NET Core MVC, the HTML for each page is served up from the /Views directory (by the Home controller by default), not from the /wwwroot, and it is C# HTML (.cshtml) just like the _Layout.cshtml file. Things like images, static javascript etc. are what's typically located in the /wwwroot directory as these are the parts of your website that are client side instead of server-side.
Try replicating your index, about and contact HTML content as .cshtml files under the /Views/Home directory, replacing whatever's there in the template, except for #{ViewData["Title"] = "Home";} located at the top. The layout template should then serve these as the body content (where #renderbody... is in _Layout.cshtml).
If this still doesn't work I'll need a bit more info about what you've changed from the default template. Hope this helps!
Thank you James for your response. I figured it out and it's actually a noob mistake which I'm posting so that other new developers don't get caught in it.
When you want to reference the content in the wwwroot folder if you reference them without "~/" before the name of the directory it works for the _Layout page which fooled me to think it should work for other parts of the application but you should include "~/" before the folder names explicitly otherwise the static files will be un reachable.

Where are cshtml files and Controllers in Umbraco with Mvc?

I've just installed Umbraco 6.1.6 making sure to have this setting in the \Config\umbracoSettings.config file before starting the install process.
<defaultRenderingEngine>Mvc</defaultRenderingEngine>
The site has installed fine, and runs fine, but the site is still full of .aspx and .ascx files. What gives? Shouldn't it have created .cshtml files for views, layouts and Controllers and stuff?
Or have I misunderstood and that is up to the user to put in?
Cheers,
-- Lee
Some part of umbraco will still use UserControls.
Try making a new documentType, that will create a view that matches that doctype
You can overrule the default Controller by creating a Controller with the same name.
Remember to inhert from.
public class NameOfTheDocTypeController : Umbraco.Web.Mvc.SurfaceController

.NET MVC - accessing .xhtml file

I have created an Area for XForms and when I try to return view("index.xhtml") the framework resolves the view as index.xhtml.aspx or index.xhtml.cshtml.
I tried routes.IgnoreRoute("{resource}.*xhtml/{*pathinfo}"); in global.asax.
Either I am not sure what URL to use (am I still hitting the controller or going straight at the .xhtml file in the views folder?) OR I made a mistake in my ignoreroute.
Any help appreciated.
If you are trying to have the action just write the content of index.xhtml, you'll need to do return File("index.html", "application/xhtml+xml"). View/PartialView assume you want the specified view file parsed and executed using the currently configured view engine.
You can't/shouldn't put static files you want remote users to be able to hit directly in your ~/Views folder. MVC places a web.config file in this folder that prevents files in this location from being served.
So, either have your controller action return the file as I mentioned above, or move the xhtml files into some other folder in your application that is not restricted. Then your route should work and your files should be served statically.

Calling Partial View from different projects

Within the project I'm using this code #Html.Partial("../ReferenceChangeLog/ReferenceChangeLogPopUp") for calling the controller and action.
Here ReferenceChangeLog is Controller Name. ReferenceChangeLogPopUp(ReferenceChangeLogPopUp.cshtml) is Partial View Name.
How can I set the path for calling another project's controller and action ?
That's not supported out of the box. By default Razor views are only resolved within the current project which is what you deploy as an ASP.NET application in IIS. If you need to implement this, you will have to write a custom Razor view engine capable of retrieving views from arbitrary locations on the file system. Here's, for example, an article which explains how you could embed Razor views into assemblies and reuse them across multiple projects.

Render View (or Partial) In another project?

i have a solution with the following two projects - MyNamespace.Services and MyNamespace.Web.
Web contains a MVC web application.
In the Service project i have a EmailService class that takes care of sending out emails to the user.
I want to use either a partial or a view (ascx or aspx) for email templates.
I have found several solutions on how to render a partial view and get the result as a string which works fine if the template is inside the web project (as it is a controller in the web project that calls the email service).
(the 2 methods i am trying to use is either http://developersisland.blogspot.com/2009/01/renderpartial-to-string-in-aspnet-mvc.html (at the bottom of the blog) or http://www.brightmix.com/blog/how-to-renderpartial-to-string-in-asp-net-mvc/)
But my email templates are located in the Services project.
How can i refference the path to the templates (partial/view) in my Service project from inside the Web project, that works with either LoadControl or RenderPartial which both takes a virtual path as a parameter ?
It seems like no matter what i do the root directory is set to the Web projects directory.
Is it possible ?
Would be nice to be able to make it work independently of the web project somehow.
I don't think this is possible without developing your own view engine. The default view engine will only look in certain locations for the partial view -- which includes the current view folder and the shared views folder. I don't think you can search for views outside the current project since those views aren't registered with the view engine.
You can consider just creating your HTML helpers to render emails and return it as a string.
Doesn't really matter whether it is partial view or a method returning a string with HTML. i actually think that for your case helper methods would be a better choice.
A simple helper method is also more flexible in the ways you can use it.
You could try creating a custom view engine locator or virtual path provider. Here are a few examples that may help you get going:
Views in seperate assemblies in ASP.NET MVC
Grouping Controllers with ASP.NET MVC
How to use virtual path providers to dynamically load and compile content from virtual paths in ASP.NET 2.0
All of the links above are good, this might help as well. you will certainly be able to get it to find and use the views. The problem I had was in working with them, there was no code completion etc in the other projects. It was semi possible to get that as well by fiddling around with the project file but to be honest I ended up going with the Grouping solution above
Plug in architecture for ASP.NET MVC

Resources