I built a console application in Visual Studio 2013, that sends email reports daily. I am using a .cshtml template parsed with Razor.
Is it possible to use partial views for my main cshtml file?
I tried using the syntax:
#Html.Partial("_partial")
but I get an error ("The name 'Html' does not exist...").
I found information about partial views only with MVC projects. I want to know how and if I can use them in a console application.
I also tried to render the partial view, to a string inside the cshtml main template, but my template will read html markup as literal string. And I can't seem to use HTML helpers outside of MVC.
Thank you in advance.
You will need to reference System.Web.Mvc.Html and then set the template base.
Razor.SetTemplateBase(typeof(HtmlTemplateBase<>));
I resolved this issue and I will post answer here, in case there are others that would benefit.
I implemented the Partial method that would render the partial view, and then set the new template base.
public class ExtendedTemplateBase<TModel> : TemplateBase<TModel>
{
public string Partial<TPartialModel>(string path, TPartialModel model)
{
var template = File.ReadAllText(path);
var partialViewResult = Razor.Parse(template, model);
return partialViewResult;
}
}
Related
I'm learning Asp MVC.
I've been doing WPF MVVM programs for two years already, but i also need to learn ASP which is a common language used in web development in my country as far as i know. And i have also knowledge in c# so i think adjusting will not be very hard, but i'm already facing a lot of problems in making my website work. I tried reading about ASP and MVC but i learn by doing things and from my mistake than reading it. So i decided to give it a try.
I created an EMPTY MVC project using Visual Studio Community Edition 2017
I already created the Layout Page and the First Controller and the First View and its totally working fine.
This is the screenshot
Then i create the second controller. Then the problem comes in.
I created a new controller named NewPostController and ADD View for it like this
But it create another folder with the name of the View and inside it is the view it created
I don't want it to organize that way.
So i dragged the NewPost.cshtml into the admin folder. Run the application then i received an error saying
The resource cannot be found.
Requested URL: /Admin/NewPost
I did a search for a solution but i can't solve the problem
I tried specifying the view name
public ActionResult NewPost()
{
return View("~/Admin/NewPost");
}
Most of the solution i read is specify the View Name. But i can't make it work. What are the things that i missed? Or not understand? Thank you.
MVC have sort of a naming convention where if your controller is named FooController then your views should be keep in a folder name Foo.
Inside this controller you will have your
public ActionResult <name of view>
name exactly the same as the view for easy referencing.
So when you have a view under the Foo Folder and the name of that cshtml file is Hello
then inside the FooController, you have a
public ActionResult Hello(//parameter here){
//body here
}
Hope you understand my explanation.
Also to answer your question. I'm assuming you want the NewPost.cshtml as part of the admin folder. Just add
public ActionResult NewPost()
to your admin controller and then you can use
localhost/admin/NewPost()
If i miss anything or any error, please comment hehe answered this in a bit of a rush
Just move your NewPost action to your AdminController as such:
public class AdminController : Controller
{
public ActionResult Dashboard()
{
return View();
}
// Here you go
public ActionResult NewPost()
{
return View();
}
}
This is default MVC structure if you want both Dashboard and NewPost views to be in the Admin folder
In an ASP.net Core web application, I'm printing a partial view from within a view using:
#Html.Partial("~/some/absolute/path.cshtml")
yet attempting to procure the view as a string from controller, the view is null after
var viewResult = razorViewEngine.FindView(actionContext, "~/some/absolute/path.cshtml", false);
//viewResult.View will be null
Has anyone encountered this? Rendering a view to string was slightly different prior to NET.Core, so I'm wondering if there's some particular project configuration I'm missing.
The solution is to use razorViewEngine.GetView instead of razorViewEngine.FindView.
Hope that helps!
This question already has answers here:
How to serve html file from another directory as ActionResult
(6 answers)
Closed 5 years ago.
We have a .NET 4.0, MVC 2 project, where the HomeController looks like this:
[HandleError]
public class HomeController : Controller
{
public ActionResult Index()
{
return View("~/client/index.html");
}
}
All is fine.
But, when we start linking to .NET 4.5 and MVC 4, the runtime can't seem to find this index.html! We get this error:
The view '~/client/index.html' or its master was not found or no view
engine supports the searched locations. The following locations were
searched: ~/client/index.html
How could this be! What might we be missing here.
I've never seen MVC using straight html pages. This is a more typical setup:
Controller
[HandleError]
public class HomeController : Controller
{
public ActionResult Index()
{
return View("Index");
}
}
The view would be /views/Home/Index.cshtml. By default MVC wants views to be in the views folder in a folder corresponding to the controller name.
If you want to have the layout broken into a separate file it would typically be in /views/Shared/.
If your HTML page is self-contained you should be able to move it and rename it to index.cshtml and add the following somewhere on the page.
#{
Layout = null;
}
Take care when locating your html page. Placing it inside a view folder where, by the MVC rules, a controller would be expected to do the handling, causes errors in my testing. Here's one way to make it work, though this isn't really coding to MVC pattern:
You can then reference the page:
Notice I've stepped outside the controller/view structure (not recommended for MVC)
To make it fail, which I'm assuming is similar to what is happening for you:
(notice the html is placed in a view where we'd expect a matching controller method to serve it to a caller)...
But...
So if you insist on going this approach perhaps you can set up a content folder outside your MVC controller/view structure and place your html there. But, again, not to beat the subject to death, you could easily convert this to cshtml and serve it up via a simple method in a controller. Just my two cents' worth..
I'm trying to set the layout path in a custom ActionFilterAttribute I have written as follow:
public class LayoutInjecterAttribute : ActionFilterAttribute
{
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
base.OnActionExecuted(filterContext);
var result = filterContext.Result as ViewResult;
if (result != null)
{
result.MasterName = "~/Views/Layouts/Test.cshtml"
}
}
}
In here Test.cshtml is precompiled view (with the help of RazorGenerator) in a different project.
But it gives me the error:
The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Home/Index.cshtml
~/Views/Shared/Index.cshtml
~/Views/Home/Index.aspx
~/Views/Home/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Views/Layouts/Test.cshtml
and controller actually is simple:
[LayoutInjecter]
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
}
The error shows that LayoutInjecter is working fine. You said:
In here Test.cshtml is precompiled view in a different project.
But, Using razor views from a different (from outside the web project) is not supported out of the box. However there's a tool to precompile razor views and then you can put them in any DLL which called RazorGenerator.
The compiler can't find the specified master layout file and shows this error.
For more information look at
Precompiled Razor View Using RazorGenerator MVC and
PreCompiledViewEngine in MVC 4.
Compile your asp.net mvc Razor views into a seperate dll
Edit: How did the PrecompiledMvcViewEngine know which view to render?
PrecompiledMvcViewEngine still relies on the ASP.NET MVC Views folder convention, using relative file paths to locate the views. However, this is slightly misleading. The PrecompiledMvcViewEngine doesn’t look at physical files; it looks for the System.Web.WebPages.PageVirtualPathAttribute that the Razor Single File Generator adds to every view that it generates that includes the view’s relative file path.
Edit 2: I believe the guidance for your problem would be found in GitHub.
It works. Make sure the layout path "~/Views/Layouts/Test.cshtml" is correct.
Also, make sure that "Test.cshtml" is a layout page, and not a view / partial view.
Change result.MasterName = "~/Views/Layouts/Test.cshtml" to result.MasterName ="~/Views/Shared/Test.cshtml".
The Framework by convention looks in the ~/Views/Shared/ directory in your asp.net mvc solution for your layout pages.
It appears to me you are dynamically or at runtime selecting a master page.
I want get a content of html file using C# and the pass those into cshtml view page. is this possible to do ?
my main requirement is load a html file into TinyMCE editor content, which is located at another destination.
but once I explore about this I saw given answers like below
You cannot give a path to the setContent function directly. What you need to do is to get the html file contents backendside and send it to your page where you insert it into your editor using the setContent method.
but I dont know how to get the html file content and send it to page in asp.net mvc
However I tried like below to insert html file using jquery
setup: function(ed) {
ed.on("init", function(ed) {
$.get("myhtml.html", function (content) {
tinyMCE.activeEditor.setContent(content);
});
})}
but this once also not working for me. any suggestion would be highly appreciate
This is very simple. Instead of using it from the jQuery i'll suggest to create the MVC controller action with string return type and then use File.ReadAllText method of System.IO assembly to get the content of the html file.
public class HomeController : Controller {
public IHtmlString TyneMice()
{
return new MvcHtmlString(System.IO.File.ReadAllText("filename"));
}
}
you can call this controller and action from jQuery.
I hope this answer helps you. If you need more detail on how to read the content from html file you can find here