ASP.NET MVC User role in Angular2 - asp.net-mvc

I'am new in Angular2 and now I would like to change my website to Angular2,and in cshtml file we can show specific HTML with razor like:
#if(User.IsInRole("Admin"))
{
<span>Admin can see here only!</span>
}
And now in Angular2 there's no cshtml,my choice is use AJAX to get the role and put in a variable or LocalStorage,is there any better practice?thanks

Yes, there is.
In Angular 2 you could use if conditions as well but in a different manner.
For example:
<div *ngIf="isAdmin" > Admin can see here only! </div>
This means that this div will be shown only when the isAdmin value is true.
And the is admin true should be set on your .ts (typescript) file where you actually will have the app logic.

Related

Insert blazor component that has cascading value parameters within cshtml page

I've recently introduced blazor components into my Razor pages project. Until now, my website endpoints have used either exclusively blazor components (.razor) or exclusively razor pages (.cshtml). I am now tring to insert blazor components within my cshtml pages. The problem arrises because I have set up default cascading values, such as an Access Token string and CascadingAuthenticationState in my App.razor page. I don't know how to set these within my cshtml page, as the cshtml page does not like the or components.
I am calling a blazor component like so from a cshtml page:
...
<div>
<component type="typeof(ProcessEditor)" render-mode="ServerPrerendered" param-Id="Model.Id" />
</div>
...
How do I supply this with the needed cascading values and authentication state?

MVC Layout with Menu

I'm new to MVC and I'm trying to make use of the Layout Page So far I have the following layout page:
...
<div class="container-full body-content">
<h2>#ViewBag.Title</h2>
<div class="row">
#if (Request.IsAuthenticated)
{
<div class="col-sm-3">
#{Html.RenderAction("MenuPartial", "Layout");}
</div>
<div class="col-sm-9" style="background-color:aqua">
#RenderBody()
</div>
}
else
{
#RenderBody()
}
</div>
...
Is there a way to get MVC to just re-render the render body rather than doing a full post back and re-loading the navigation menu each time a page changes?
First, MVC doesn't do "postbacks". If you click a link, that's just a GET request. If you actually submit a form, it will send the request as a POST, but the destination could be an entirely different view or the same.
Second, taking an action like clicking a link or submitting a form within a web browser by default sends a request to the server and then entirely replaces the user's rendered view with a the response from the server. That necessitates the the server send back a full HTML document, including any associated layout.
If you want to replace just a portion of the HTML page without changing the user's view, that requires using AJAX. You send an AJAX request to request an action that will return a partial view. In other words, it will not utilize the layout you've defined for the web application. You, then, are responsible for replacing the appropriate portion of the DOM client-side with the server's response. There's a plethora of client-side JavaScript libraries that can help you manage this type of workflow, but they're all frameworks in their own right. In other words, they handle routing, models, views, etc. These are what's referred to as SPAs, or Single Page Applications. When you create a SPA, the server is relegated to a pure support role, only providing endpoints allowing you to retrieve or update data. Web Api is a popular choice here exactly for that reason; as all the MVC machinery is unnecessary.
If you're just looking to optimize things by say not having to render the child action that returns the menu, you can employ OutputCache on the child action so that for a period of time, the action will not need to run again, and the HTML output it generated can just be dumped directly into the appropriate place in the layout.

Direct BeginUmbracoForm action to another page

I am trying to set up a global search box that posts to a search page in umbraco 7. This template uses a shared partial containing Html.BeginUmbracoForm() that points to a SurfaceController.
Since this is a global control, I want the form to be posted to the /search page instead of the current page. BeginUmbracoForm only seems to be able to post to the current page unless I'm mistaken.
I want something like RedirectToUmbracoPage(id), but clears this post values on redirect.
Is there a way to get an ActionResult like CurrentUmbracoPage which keeps the post values?
I would not try using surfacecontrollers for that purpose. Surfacecontrollers are auto routed. They are created to (e.g.) make #Html.Action(...) work on every umbraco page. Including postbacks on these actions or macros.
If you only need a "redirect" to the /search page, you don't need a surface controller. A simple partial will do the job.
<form action="/search" method="get">
Search <input type="search" name="q" />
</form>
Of course you can replace the url using some Umbraco logic like #Umbraco.TypedContentSingleAtXPath("//Search") or other Umbraco magic.
In the umbraco documentation project is an example where Html.Action is used.

Understanding MVC tags for nopCommerce

I am new to MVC , and in the application i downloaded and trying to debug i see this mark up
#Html.Widget("body_start_html_tag_after")
#Html.Partial("_Notifications")
#Html.Action("AdminHeaderLinks", "Common")
What does this mean?, #Html.Partial where can I find where the value "body_start_html_tag_after") is defined ?
And this one:
<div class="master-wrapper-main">
#RenderBody()
</div>
Where can i find what #RenderBody does?, this is in a .cshtml file.
I would suggest that you look at a reference like http://www.asp.net/mvc to gain a greater understanding of ASP.Net MVC. Having said that the #HTML.Widget, etc is server side code that gets called during the HTML generation process.
I have heard of nopCommerce but I am unfamiliar with the structure, but #Html is usually used for server side helper methods.
#Html.Partial("_Notifications") is used to add the _Notifications Partial view to the page being rendered.
#Html.Action method will render a html A tag with the href link to the controller and action to be performed.
#Html.Widget I am unfamiliar with but one could assume that it is a helper method.
#RenderBody is used on a master page (usually the shared/_Layout.cshtml) as a server side marker to render the view that comes from the associated controller.

ASP.NET MVC urls for subsites

In my Views I have urls mapped like /My/Urls.something, for example in links (when I don't use View helpers), including static content like images, and mostly in javascript code (Ajax calls and the like).
This works fine when I deploy my app in a first level domain, like http://mysite.com/ because the / before the Url remaps it to the root of the domain, but if the site is deployed as a subsite, for example http://mysite.com/myapp/, this doesn't worka anymore, and omitting the / (for example /My/Urls.something -> My/Urls.something) doesn't work in inner pages like http://mysite.com/myapp/admin where I want to read http://mysite.com/myapp/My/Urls.something but I get http://mysite.com/myapp/admin/My/Urls.something.
Any help?
You should always use view helpers. Even if you don't want to use the ActionLink helper, you should use the Url.Action helper to generate your url. Why? Because of the very problem you're trying to solve, which the helpers were designed to fix.
For example:
<a href="#Url.Action("Index", "Home")" />

Resources