Change Default Page in ASP.NET Core Razor Page to Login Page - .net-core

I am changing Default Page from /Home/Index to /Identity/Account/Login using ASP.NET Core Razor Page but it is always loading /Home page as default page.
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
.AddRazorPagesOptions(options =>
{
options.Conventions.Clear();
options.AllowAreas = true;
options.Conventions.AddAreaPageRoute("Identity", "/Login", "/Identity/Account/Login");
});
It should load the Login Page Model as default page. Please help me to solve above problem. Waiting for your quick response.

To map your site's root to the /Identity/Account/Login page in ASP.Net Core (v2.1 and above) you can follow these instructions:
Undo the changes you've made in the ConfigureServices() method. (I.e. .AddRazorPagesOptions(...))
Change the #page declaration at the top of the Login.cshtml template file to #page "/"
If you haven't already, delete the Index.cshtml and Index.cs page files.
Step 1 is for cleaning up.
Step 2 is to configure the Login to the root path /. (more info here)
Step 3 is necessary because if the Index page files are not deleted, they will be mapped by convention to the root path and will conflict with the changes in step 2. This will throw a AmbiguousMatchException when you browse to the root path.

The arguments of AddAreaPageRoute are areaName, pageName, route
For setting the default page in area Identity to /IdentityAccount/Login you would have to call
options.Conventions.AddAreaPageRoute("Identity", "/Identity/Account/Login", "");
If you want to set the page-wide default page you should call
options.Conventions.AddPageRoute("/Identity/Account/Login", "");
The default page is at route ""

Related

How can i use classic MVC routing in razor pages?

I have set the url in my categories page as
<a href="/Shared/product/#p.Id">
</a>
My product page physically exists in the Shared directory.
The page directive of the view is set as
#page "/product/{id}"
and the OnGet method as
public void OnGet(int id)
{
}
Why do i get an 404 error? How can i fix this one?
I would like to generate the url as /product/1 for example
I have tried setting the url as
<a href="/product/#p.Id">
</a>
but i get 404 also
You have overridden the default route to the page (https://www.learnrazorpages.com/razor-pages/routing#override-routes), which is generated from its file path (rooted in the Pages folder). Change the template to "{id}", representing only the route parameter. The file path (/shared/product) already matches the route that you want.
You should only override the route if you don't want the URL to match the file path of the page.
Incidentally, the Shared folder in an MVC or Razor Pages app is intended only for partials and layout files, not pages.

Using ASP.NET 5, hide index.html in url

I've followed some basic tutorials to create ASP.NET 5 with MVC 6 and AngularJS applications.
I've got a setup like so:
index.html
app.js
When I load the project, I have to go to index.html, or set it as the Launch URL to naturally visit my webpage. this means that the url browser shows:
http://localhost:16510/index.html
How can I set up the ASP.NET 5.0 project so that when the user goes to / it views index.html?
e.g.
http:localhost:16510/
You should put the code of index.html in the /Views/Home/Index.cshtml.
In the startup.cs you have your default route configuration.
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
This tells that your default controller is Home and default action method is Index. So whenever you accesss your site without a controllername & action name in your url, the request will be send to Home/Index. Your url will be http://yourSiteName

Unable to change nopCommerce default start page

I am trying to get my nopCommerce 3.50 site to open a specific page when the site is visited.
When I debug the project locally it works correctly as I have the specific page 'welcome.html' set as the start action in the project properties (in Visual Studio 2012). However once the site is published on the server it ignores this request.
In IIS I have checked that 'welcome.html' is at the top of the list of default pages, however nopCommerce ignores these settings (as found on various forums) and even if you delete everything else it always goes to default.aspx.
I've tried adding this to the web.config in system.webserver and this has made no difference:
<defaultDocument>
            <files>
                <add value="Welcome.html"/>
            </files>        
</defaultDocument>
There is no other reference to a default starting page in the config.
I'm not too familiar with nopCommerce or the routing, does anyone know how/where I can change the default page? Everything I have found on the forums so far has not worked. And yes, I have rebuilt the project after each change, usually completely replacing the bin folder on the server to make sure.
I don't want the site to redirect to a specific action or view, but to a static HTML document in the root folder of the project (well I don't want that either really but the client has been very specific about what they want). The static page has two buttons, one that will go to the nopCommerce default.aspx and one that goes to another site.
Thanks
you can change the route in the global.asax.cs
Default route is
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new[] { "Nop.Web.Controllers" }
);
This route will tell you execute in the HomeController, there is Index action (function) and return the view defined with the same name under the Views. View path will be Views/Home/Index
In order to change that you should add a welcome function (just copy the Index function and rename with welcome and paste it into the HomeController).
then you can change your routing as bellow.
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Welcome", id = UrlParameter.Optional },
new[] { "Nop.Web.Controllers" }
);
Dont forget to create your Welcome view under Views/Home as Welcome.cshtml
Found a way round it to meet the requirements:
Add a new folder into the directory ('NOP' for example) and put everything apart from the welcome.html page inside the new folder.
Change the links in the welcome page to point inside the new folder structure (images, links etc.).
In IIS make sure that welcome.html is set as a default document, and as it is now the only document in the folder it loads correctly.
In IIS convert the new folder into an application (right click > 'convert to application' on Windows Server 2012 R2)
This way no changes are needed to the code in nopCommerce and the desired result is achieved.

Register Route and javascript problem

I registered a Route in my route table in the global asax file. When I load the page it loads normally without the new route and my menu works fine, but when i enter in the new url for the route it gives me a javascript error(object expected) for my jquery menu. error is on the
document.ready(function() {})
i've tried changing the order of my scripts but i still get the same problem no matter what.
Im using a master page for my ShowProducts page. When i view the source and have a look at the links to my scripts they still point to the directory where they reside. When you change the url to the mapped route does the url change for the source of the script files.

asp.net site default document in subfolder

My default document is in subfolder not in root how can i make it default in asp.net 2.0 website.
Tried iis7 default document setting to '/pages/default.aspx'
'~/pages/default.aspx' but it didn't work.
Default document is not the same as start page. Default document means if I requested mysite.com/somefolder and didn't specify a file, which file should IIS display.
If you want to use a specific page as your home page, create a Default.aspx file and write this in it's codebehind class:
public override void ProcessRequest(HttpContext context) {
context.Response.Redirect("pages/default.aspx", true);
}
As the client might have disabled Javascript, a server side approach would be more reliable. However it's best to issue a permanent redirect instead of a simple Response.Redirect. Also doing it using JS will be bad from a SEO point of view.
You don't need to create a dummy Default.aspx page.
In your Global.asax.cs file, write the following:
public void Application_Start(object sender, EventArgs e)
{
var routeCollection = RouteTable.Routes;
routeCollection.MapPageRoute("DefaultRoute", string.Empty, "~/YourDesiredSubFolder/YourDesiredDocument.aspx");
}
Explanation:
Application_Start code is guaranteed to run once and only once on the application start.
The first line of code, gets a collection of the URL routes for your application.
The second line of code, defines a new route pointing to your inner page in the subfolder that you wish.
The second argument is empty to indicate that this route is used when there's no specific page is requested and there's no Default document existing.
Default documents are a subfolder-specific thing - what you're trying to do won't (directly) work. Set up a default.htm file in the root, and have it refresh to your real "home page".
The better question you should be asking is how on Earth your homepage got out of the root directory.
In theory you could have a Web.config file inside the directory and use the defaultDocument element to set the default document. See here: https://stackoverflow.com/a/2012079/125938.
Unfortunately I haven't been able to get it to work myself locally, but that might be because it isn't supported in the Visual Studio development server.
Say "index.html" is the default page you want and it is present in "Public" subfolder.
Instead of specifying "/Public/index.html" as the default site, try "Public/index.html"

Resources