Paths searched for ViewComponent files in RazorPages app? - razor-pages

.NET Core 3.0 Razorpages app
ViewComponents folder
- My viewcomponent class is here. Derives from ViewComponent and returns
return await Task.FromResult((IViewComponentResult)View("Default"));
Pages/Components/MyViewComponent
- Detault.chtml, this is the view template to be used by my viewcomponent
I have a breakpoint set within InvokeAsync where I am trying to return the view and thats when I get the 404
Are the path locations right?

Pages/Components/MyViewComponent
- Detault.html, this is the view template to be used by my viewcomponent
The view part of a ViewComponent should be a Razor file, not a plain html file. It should be Default.cshtml.
Ref: https://www.learnrazorpages.com/razor-pages/view-components

Related

In Wicket 9.x, how do a include javascript and css via Behavior renderHead(Component component, IHeaderResponse response)?

I have this code that works in Wicket 8.x but does not work in Wicket 9.x:
...
public class FancyComboBoxBehavior extends Behavior
{
...
#Override
public void renderHead(Component component, IHeaderResponse response)
{
response.render(CssHeaderItem.forReference(CSS_REF));
response.render(JavaScriptHeaderItem.forReference(JS_REF));
if (initJS)
response
.render(JavaScriptHeaderItem.forScript("$('select').selectBoxIt();","fancyComboBoxInit"));
}
...
}
Like I said, in Wicket 8.x, I add this behavior to a page, then when I go to that page in the browser, right click on the page and pick View Source, I can search the page for .js and find the javascript file defined in JS_REF, I can search for .css and find the css file defined in CSS_REF. I can search for selectBoxIt() and find the <script/> tag added to the response. In Wicket 9.x, I cannot find either of the files nor can I find the <script/> tag for the .selectBoxIt() call.
by default Wicket 9 has a CSP policy active by default. This prevents inline JavaScript and CSS code from been rendered and executed. You can easily disable CSP adding the following line to your app init() method:
public void init() {
getCspSettings().blocking().disabled();
}
For more info check the migration guide here:
https://cwiki.apache.org/confluence/display/WICKET/Migration+to+Wicket+9.0

AngularJS 2 with .NetFramework 4.6

I'm currently making a web application with Asp.net MVC 5 (.Net Framework 4.6) and AngularJS 2.0 .
I take care the back-end and my friend takes care the front-end.
Now, he sends me 3 js files, 1 html file.
Can anyone help me to import those file to MVC 5 please ?
Thank you,
I´m assuming you are new to MVC...
Create a new MVC project.
Copy your js files to Scripts. (Drag from explorer to visual studio)
The best way to go with js is to make a bundle:
In app_start, open BundleConfig.cs
Create a new Bundle like this:
bundles.Add(new ScriptBundle("~/bundles/mybundle").Include(
"~/Scripts/myjs1.js",
"~/Scripts/myjs2.js",
"~/Scripts/myjs3.js"));
Now to render this bundle, open Views->Shared->_Layout.cshtml
Go to the bottom, find #Scripts.Render("~/bundles/jquery")
Add your bundle: #Scripts.Render("~/bundles/mybundle") Now, your js is available to all pages.
We need to create a controller. Right click Controllers folder -> add -> Controller
Select MVC5 Controller Empty.
Give it the name you like for this page.
You will see this:
public class testeController : Controller
{
// GET: teste
public ActionResult Index()
{
return View();
}
}
11. Right Click the Index in the code -> click in add View
12. VS will open the view. Paste your HTML here.
You are good to go!
Things to keep in mind 1 - ROUTES:
MVC default route (URL) is /controller/Action/id
In the example above, your url will be: /teste or /teste/index because it´s the controllers name.
If not provided MVC uses Home as controller name and Index as action name and id is optional.
So, if your page is the home page for the site put it in the Views -> Home -> index.cshtml
Things to keep in mind 2 - BUNDLES:
Bundles are available for all pages, if you need your js in only one page, to this:
Open your View, go to the bottom of the file and do this:
#section scripts{
<script src="~/Scripts/js1.js"></script>
<script src="~/Scripts/js2.js"></script>
<script src="~/Scripts/js3.js"></script>
}
Things to keep in mind 2 - LAYOUT:
MVC works divides your content into 2 files, stuff common to all pages, like navigation bar, footer, css and js calls reside in:
Views -> Shared -> _Layout.cs
Views are the changeable content in the middle of the page, look for the method #RenderBody() in _Layout.cshtml to find where your view will be rendered.
Maybe you will have to divide your friend´s html to have some in _layout and some in the View. This is quite common.
Good luck and Happy Coding!!
Sure,
Create an ../app folder and drop the js files
The html file you have, I suggest you copy paste its content into a razor view, ../Views/App/MyPage.cshtml
Then create a c# controller that will return that view AppController
public class AppController : Controller
{
public ActionResult MyPage()
{
return PartialView();
}
Like this https://github.com/victorantos/AngJobs/blob/master/AngJobs/Controllers/AppController.cs
Another important note, make sure your router is configured properly, like this
https://github.com/victorantos/AngJobs/blob/master/AngJobs/App_Start/RouteConfig.cs

Sitecore.Placeholder("content") searching in wrong location for partial view

I have an MVC 5 application set up and it deploys to a Sitecore 8.0 Website. I have the Sitecore home item set up to render two "controls" - a PartialView Rendering called PageHeader and a Controller called HelloWorldController.
I am invoking these on a Layout which is assigned to the home item - the code that registers to that layout is as follows:
<p>Today's date is :#DateTime.Today.ToShortDateString()</p>
#Html.Sitecore().Placeholder("content")
For some reason, when I visit the homepage, I get this error:
The partial view '/views/renderers/Views/PageHeader.cshtml' was not found or no view engine supports the searched locations. The following locations were searched:
/views/renderers/Views/PageHeader.cshtml
However, my PageHeader doesn't live inside "/views/renderers/" . that directory doesn't exist. Does Sitecore generate this "/views/renderers/" from somewhere?
These is due to Mvc.RenderersViewFolder setting in App_Config\Include\Sitecore.Mvc.config, you need to patch the value:
<!-- MVC: Path to the base file folder that contains the views used for item renderings.
Default: "/views/renderers"
-->
<setting name="Mvc.RenderersViewFolder" value="/views/renderers" />
Patch the value to either empty string or whatever your views folder is.

Yii layout in controller

i'm a new one in Yii :)
I'm trying to generate CRUD structure with Gii and everything ok.
But i'm a little confused about overriding layout path in controller.
public $layout = '//layouts/column2';
How to explain such path, especially "//" part?
Have a look at the documentation for CController::getViewFile(). It explains the // and other view path options.
It means that the template file column2.php has to be found under the default view directory for the application. That is : protected/views

asp.net webpages content block and helper differences

In asp.net webpages framework what is the difference between using a content block versus a helper?
They both seem to be used to output HTML to multiple pages. They both can contain code and both can pass parameters.
Are there other differences? When should you use a helper versus a content block?
More info:
With Content Blocks we create a .cshtml (for example _MakeNote.cshtml) file to hold the content we want to insert into a page. Then we use:
#RenderPage("/Shared/_MakeNote.cshtml")
to insert the content into a page. We can pass parameters to the content block like this:
#RenderPage("/Shared/_MakeNote.cshtml", new { content = "hello from content block" })
It's somewhat like an include file, but I think does not share scope with the parent page.
With Helpers we create a .cshtml page in the App_Code folder (for example MyHelpers.cshtml) and place methods in that page which we want to call. The method looks something like this:
#helper MakeNote(string content) {
<div>#content</div>
}
The helper is called by using:
#MyHelpers.MakeNote("Hello from helper")
There isn't a lot of difference functionally. Helpers need to go into an App_Code folder - unless you download VWD or Visual C# Express and compile a binary - and the App_Code folder doesn't translate well to the MVC framework. Of course, that's only relevant if you want to upgrade to MVC at some point.
I would use a helper for generic functional snippets like your MakeNote. I would use a "content-block" (partial, really) for repeated site-specific sections of a page.

Resources