I am confused on how to call or create server/controller methods in razor pages. When I do MVC, I would create methods in the controller and would use an anchor tag to call it like this:
<a href="/Security/LogOut/">
How do I do this in razor pages? I ended up creating a new page name "SignOut" and implemented the login inside the OnGet and used an anchor tag like this:
<a href="/SignOut">
What if I have more than one action method I want to group in a file? Do I need to create a page for every action?
You've probably long since found the answer you were looking for. For others that see this question, you can use:
<a asp-page="/SignOut">Sign Out</a>
In the case of Razor Pages, the GET and POST methods are handled by each code-behind file. For instance, for the Razor Page Details.cshtml, there is a code-behind file called Details.cshtml.cs that defines OnPostAsync and OnGetAsync which handle POST and GET requests respectively. For more information, see:
Microsoft Razor Pages Tutorial
Related
Within a controller called AccountController, I have a method MeetingLog() that gathers the info I need and it stores it within a ViewModel. I then take that ViewModel called meetingInfo and try to pass it into the view/razor page using "return View(meetingInfo)". Normally, to access the ViewModel I would have gone right into a Views folder and insert "#model ITMatching.ViewModels.MeetingLogViewModel" at the top of MeetingLog.cshtml file. Unfortunately, this razor page is located within the "identity pages" and requires a bit more work to access the ViewModel.
The problem with this specific directory "Areas/Identity/Pages/Account/Manage/MeetingLog.cshtml" is that makes using the anchor tag helper attribute (asp-controller) hard to use in the _ManageNav.cshtml file.
Image of the identity directory the view is located in
The example I'm working with:
code of where asp-controller is used
asp-controller could have been accessed using asp-controller="Manage/Account" but it always converts the '/' into "%2F" making the page inaccessible. And only using asp-controller="Account" ignores looking into the "Areas/Identity/..." directory when that's what I need it to do.
To pass a ViewModel into view within Identity pages or between two requests, you could use Sessions or TempData(backed by session state. After enabling the session middleware, you could store the object (need to add the session extension or TempData extension) using session and TempData, then in the Razor Page OnGet method, you could get value from the session and TempData and assign it to the View Model.
More detail information, check the following links:
Sending objects(And Child Objects) from a Razor page to another
Session and state management in ASP.NET Core
asp-controller could have been accessed using
asp-controller="Manage/Account" but it always converts the '/' into
"%2F" making the page inaccessible. And only using
asp-controller="Account" ignores looking into the "Areas/Identity/..."
directory when that's what I need it to do.
For the Areas, if the folder structure using MVC folder structure(contains Controllers and Views), then, in the View page, you could use asp-area, asp-controller and asp-action attribute to generate the link:
<a asp-area="" asp-controller="Home" asp-action="About">/Home/About </a>
For the Razor page folder structure, you could use asp-areas and asp-page attribute add the hyperlink:
<a asp-area="Products" asp-page="/About"> Products/About </a>
Besides, you could also directly use the href attribute:
<a class="nav-link" href="/Identity/Account/Manage/Index">View Meeting Logs</a>
More detail information about areas, see Areas in ASP.NET Core
[Note] For the protected pages, it required the user to login first, so, it will redirect the Login page. For example: https://localhost:44310/Identity/Account/Login?ReturnUrl=%2FIdentity%2FAccount%2FManage%2FIndex. After login success, it will redirect to the Manage/Index page.
I am new to Razor Pages. I have just one page, I have Index.cshtml and Index.cshtml.cs with
public class IndexModel : PageModel
There is also _Layout.cshtml.
I need to get username from HttpContext Header. and show username in _Layout.cshtml (there is navigation bar).
But I do not get where is the best place to put this code in Razor Pages?
I cannot put it into Index.cshtml.cs OnGet, because I need in Layout, at least I think so.
This is outside this question, but FYI I use Azure Easy Auth and from documentation (and also other non Razor Pages projects) I see that HttpContext.Request.Headers["X-MS-CLIENT-PRINCIPAL-ID"] would contain Name I need.
You can access the current HttpContext inside of a Razor Page using the Context property:
<p>#Context.Request.Headers["X-MS-CLIENT-PRINCIPAL-ID"]</p>
This approach is documented in Use HttpContext from a Razor view.
#Context is not available in razor page in .NET 6
But you can use #Request.HttpContext
I am building a .NET MVC 5 application on back-end and Angularjs on front-end.
I am loading .cshtml views in a div containerOne on a parent .cshtml page with ui.router and everything is working fine. An issue I would like to solve is when I enter manually a page URL that is C# controller's action path(in the example I provided below it is /Proposal/Customers) - my view is loaded on a whole page. What I want to be called is a .state named 'customers' in my example, or something like that. My example code is(part of my proposalConfig.js):
.state('customers', {
url: 'AllCustomers',
views: {
containerOne": {
templateUrl: '/Proposal/Customers'
}
}
});
On my back-end I have a ProposalController.cs and an action method Customers that calls a Customers.cshtml view.
Anyone has an idea how to solve this?
EDIT
The same thing happens if, instead of 'AllCustomers' I put '/Proposal/Customers', and then after the first load of a .state I refresh a page.
I forgot to mention that I have $locationProvider.hashPrefix('!').html5Mode(true); in a proposalConfig.js file.
If you mean that the entire page html markup (html tag, body tag and such)is being returned when you only want the specific content of the Customer.cshtml, which I also assume only has what you want in it, its probably because your view has a shared view start layout. Put this in Customer.cshtml
#{
Layout = null ;
}
I've read some previous questions here and on other sites, but being new to ASP.net and MVC I'm having a bit of trouble understanding the information presented.
I want/need/was told to get the current URL of the page I'm on through the View layer, and use that information to apply an id to a li tag allowing for specific css. We've moved our left navigation bar from being embedded in every single page (done by a previous co-op) to putting the list in a partial view that I'm going to call on all of the required pages. Styling requirements for the site have a specific highlight on the left navigation a tag of the page the user is currently on.
Some of the examples I've read including using:
<%= Request.Url.PathAndQuery %>
Request.Url.ToString() or Request.Url.AbsoluteUri
var request = HttpContext.Current.Request
but I know not all of them can be used in the View layer. What would be the best approach? Are there any tutorials that I haven't been able to find yet that anyone could recommend?
It probably isn't the best idea, in my opinion, to use the URL for this.
Instead, a quick and easy way to achieve this is to use ViewContext.RouteData that will contain values for both the controller and action of the current request. It can be accessed from the view layer easily.
ViewContext.RouteData.Values["Controller"].ToString()
ViewContext.RouteData.Values["Action"].ToString()
So in your view you could do something like
<ul class="nav">
<li class="#(ViewContext.RouteData.Values["Controller"].ToString() == "ControllerName" ? "active" : "")">Foo</li>
</ul>
You could push it further to make it prettier, but you get the basic idea.
I was looking for a solution on this for asp.net 3.0 and found the following to give direct access to the URL path (what page you're currently loading/showing):
ViewContext.HttpContext.Request.Path
I'm using this in an if statement to decide dynamically between different View Imports, works very well.
I am having a hard time figuring out how to redirect to an outside source.
in my code, I have
<%= Html.ActionLink("New Name Search", "Index") %>
which will allow me to navigate within the code.
how do I redirect to ...google for example?
google
The purpose of the ActionLink helper is to generate links that will direct the user to a controller action that you've defined.
If you want to navigate the user to an outside source you should just use a regular anchor tag.
Response.Redirect("http://google.com/");
(It's not really MVC-specific, by the way)
If you are redirecting from your controller (or action filter, etc.) you can use the RedirectResult as your ActionResult type:
RedirectResult("http://www.google.com");
This is essentially doing a Response.Redirect, but is the preferred way of sticking with ASP.NET MVC conventions.
If you are just creating a link inside a View, just use Click to go to Google.
"Redirecting" means many things.
If you just want to show a link that redirects the user to another URL you can use the anchor tag normally in you templates.
google
Now, if you want to redirect the user within the controller, call the Redirect Method.
Result("http://www.google.com");