How do i change startup url path of Razor Page Application so it can load a specific .cshtml - asp.net-core

I want to load my application at http://localhost:52856/CRUD/Products/List so then i can start working on the List.cshtml file instead of loading Index.cshtml at http://localhost:52856 .
Look i know i can just put a button at the index file so then it can redirect to path or use the navbar, but personally i don't want to do that every single time.Just load the application at the file.cshtml i want. But How can i do it?

If you don't like my comment you could do the following in Index.cshtml.cs
public IActionResult OnGet()
{
return new RedirectToPageResult("/CRUD/Product/List");
}

1. Include MVC as a service into our app.
we can optionally use another method called AddRazorPagesOptions() like so:
services.AddMvc().AddRazorPagesOptions(options =>
{
options.Conventions.AddPageRoute("/Customer/Index", "");
});
Within that AddRazorPagesOptions() method, we can set things like route conventions and the root directory for pages. It turns out that, to set a default route for a page.
2. remove or rename Pages/Index.cshtml

Related

How to RedirectToPage to ChangePassword in razor pages identity

I can't use RedirectToPage for any of the pages in the identity folder in my razor pages app.
If I use: return RedirectToPage("/Identity/Account/Manage/ChangePassword");
What I got is an error:
InvalidOperationException: No page named '/Identity/Account/Manage/ChangePassword' matches the supplied values.
In the case of using Redirect itself, routing works fine but I need to route some values so I have to use RedirectToPage
If you want to use RedirectToPage, try the below code:
return RedirectToPage("/Account/Manage/ChangePassword");
/Account/Manage/ChangePassword means that we are looking for the ChangePassword.cshtml file in the Manage folder in the Account folder of the Pages folder.
return RedirectToPage("/Privacy");
/Privacy means that we are looking for the Privacy.cshtml file in the Pages folder.
The RedirectToPage() method is referring to a Razor cshtml "page" rather than a html page in the browser. So it expects the parameter to refer to a cshtml page in your project.
update:
If you add in IndexModel class to call page in Indentity folder you can try :
public IActionResult OnGet()
{
return RedirectToPage("/Account/Manage/ChangePassword", new { area = "Identity" });
}

Razor Pages Default Page in aspnetcore 2

By default a Razor Page app goes to Home/Index
Is there a way to change this to Home/App?
This is quite easy in MVC, but Razor pages using a different routing setup and thus MVC routing does not apply.
I would think it would be in options somewhere, but I don't see it:
services.AddMvc()
.AddRazorPagesOptions(options =>
{
options.Conventions.AuthorizeFolder("/Account/Manage");
options.Conventions.AuthorizePage("/Account/Logout");
options. ??SetDefaultPage??
});
I have tried this:
options.Conventions.AddPageRoute("/App", "");
But now two default routes are found and an error is generated:
AmbiguousActionException: Multiple actions matched. The following actions matched route data and had all constraints satisfied:
Page: /App
Page: /Index
It's possible to resolve this error by removing Pages/Index.cshtml from the project, but I wanted to keep that page as well.
In my case the ambiguity was caused by Pages/Index.cshtml left in project.
This worked:
options.Conventions.AddPageRoute("/App", "");
remove or rename Pages/Index.cshtml
Pretty sure it isn't possible. The docs say the runtime controls the search for Index as the default. I couldn't find where that happens in the current release, but IndexFileName is a static in the new internal PageRouteModelFactory class added to the upcoming release:
private static readonly string IndexFileName = "Index" + RazorViewEngine.ViewExtension;
It doesn't seem like it would be difficult to just add a config property to RazorPagesOptions, though. The ASP.NET guys are pretty responsive, I'd open a request as a GitHub issue and hope for the best.
I solved the issue by using Microsoft.AspNetCore.Rewrite:
Then adding code to replace the default Index action, in my case with Portfolio:
var options = new RewriteOptions()
.AddRedirect("^", "portfolio"); // Replace default index page with portfolio
More detailed article about rewrite options - https://learn.microsoft.com/en-us/aspnet/core/fundamentals/url-rewriting?tabs=aspnetcore2x
Another way is to simply redirect from the Index OnGet method, like so:
public class IndexModel : PageModel
{
public IActionResult OnGet()
{
return Redirect("/Welcome");
}
}
Notice that I have change the return type of the OnGet method.

How to a hide a page

I have a pop up that is called through a java script, the same pop up without a JavaScript is an ctp page in cakephp. How can I hide that page from users and search engines going to access it like: /users/register
Is there anything that can be done in .htaccess or cakephp to prevent access to it through /users/register
Remove register.ctp file from users folder and create one in ajax folder users/ajax/register.ctp, then use RequestHandler component to inspect request type:
public function register()
{
if($this->request->is('ajax')){
// add registration code here
} else {
//Throw new error
}
}

Why isn't my HTML file showing up in MVC 4 when it's in the Shared folder?

Given this link:
Test
Which is an HTML page in Shared folder as shown in the href attribute. Shouldn't MVC be able to find this in the Shared folder?
When I click on the link I get a 404 error.
Found the solution and am posting for others benefit...
MVC routing is wired up out-of-the-box to NOT handle inbound .HTML requests. So why fight it?
Create an action method that looks like this:
public ActionResult GetHTMLFile(string filename)
{
var dir = "/Views/HTML/";
var suffix = ".html";
var path = dir + filename + suffix;
return new FilePathResult(path, "text/html");
}
This tells us that we have a Views folder and a subfolder named HTML where we store all of our HTML files and ensure the suffix is always html. VS does this by default when you add new HTML files. This method is "building" the full path to the HTML file. It then uses FilePathResult to return the content.
Use the ActionLink helper because you can easily configure the action method name as well as the controller name if you need it...
#ActionLink("Layout File changes", "GetHTMLFile", new {filename="_Layout"})
We are "Tricking" MVC by only passing part of the file name (without the extension and without the actual path). The controller get's the string and serves up the HTML for us.

mvc4 areas multiple route

I have a web site in MVC4 with area "admin" inside controller named "HomeController" ,also in my project a folder controller with controller named "HomeController" : when I do a call to
public ActionResult LogOff()
{
FormsAuthentication.SignOut();
return RedirectToAction("Index", "Home");
}
I get error :
Multiple types were found that match the controller named 'Home'. This can happen if the route that services this request ('{controller}/{action}/{id}') does not specify namespaces to search for a controller that matches the request. If this is the case, register this route by calling an overload of the 'MapRoute' method that takes a 'namespaces' parameter.
The request for 'Home' has found the following matching controllers:
site1.co.il.Controllers.HomeController
site1.co.il.Areas.Admin.Controllers.HomeController
If you have the same controller and action in different areas, MVC4 has no way to choose one of them, unless you specify which is the desired route.
You can specify it like this:
return RedirectToAction("action", "controller", new { area = "area" });
In your case area should be "admin"
If you want to refer to the root Controller, area should be ""
You can also try to refer to the appropriate controller like this: "admin\home", but I don't know if the equivalent "\home" would work.