its showing a blank Page instead of Home/Index.csml file.
Question-1: How can i change the default folder in Razor Pages?
Now, I created a create post form with slug input. that means i have slug and CategoryID stored in Db where Post have relationship with category model with CagoryID and Category type Model. My Post url looks like localhost:***/Home/Post?id=4 or /Home/Post/4.
I have used asp-page and asp-route-id tag helpers to do that.
Question-2: What i want is Localhost***/CategoryName/slug
My Post.cshtml.cs get code:
public async Task<IActionResult> OnGetAsync(int? id)
{
if (id == null)
{
return NotFound();
}
SinglePost = await _context.Post
.Include(p => p.Category)
.AsNoTracking()
.FirstOrDefaultAsync(m => m.PostID == id);
return Page();
}
It would be really helpful for me. Please help me.
The root directory name doesn't form part of the URL. Pages are rooted in the RootDirectory. Your Home/Index.cshtml page is accessible at / or /Index. If you want to have a url that includes Home, remove the RootDirectory configuration, add a Home folder
to Pages, and then add your pages there.
i tried the Learnrazorpages website methods. its not working. and i think that instructions are for 2.2. please read my question again. after adding Home to root directory the whole web app shows blank page.
Please read the question. Its in the code that i tried to change it to /Home directory.
You misunderstand the RootDirectory configuration. RootDirectory maps a relative directory to the application root. The RootDirectory is not shown in the URL because it is the application root. For example, the user goes to
http://www.myapp.com. The framework returns /Pages/Index.cshtml.
In Pages Folder I created a Home Folder which i want to make it default folder to show in home link like myapp.com. and for single posts(/Home/Post.cshtml) and category(/Home/Category.cshtml) pages, i want to change urls from myapp.com/Post?id=1 to myapp.com/technology/how-to-buy-a-mobile
and myapp.com/Category?id=1 to myapp.com/Technology. I have slug and category in the post database as i described in the question. so, i don't want to convert post title into slug. i just want to change the url.
My Single Post cs code:
public Post SinglePost { get; set; }
public IList<Post> RelatedPosts { get; set; }
[BindProperty]
public AppUser AuthorBio { get; set; }
public async Task<IActionResult> OnGetAsync(int? id)
{
if (id == null)
{
return NotFound();
}
SinglePost = await _context.Post
.Include(p => p.Category)
.AsNoTracking()
.FirstOrDefaultAsync(m => m.PostID == id);
RelatedPosts = await _context.Post.Include(p => p.Category).Where(p => p.CategoryID == SinglePost.CategoryID)
.AsNoTracking()
.ToListAsync();
if (SinglePost == null)
{
return NotFound();
}
AuthorBio = await _userManager.FindByIdAsync(SinglePost.AuthorId);
return Page();
}
i can add string slug in ongetasync class. i tried also. the only problem here is routing. and i can't find a solution because they updated routing in .net core 3.
i can add string slug in ongetasync class. i tried also. the only problem here is routing. and i can't find a solution because they updated routing in .net core 3.
I recommend that you set aside time to learn Razor Page routing. It's a bit different than MVC. mikesdotnetting, the original responder to your question, created a wonderful guide which was linked above.
I tried this, its not updated to .net core 3 i think. my question have the code from the link you suggested. its the problem with .net core 3 updated. they updated the routing in razor pages in .net core 3.
I tried this, its not updated to .net core 3 i think. my question have the code from the link you suggested. its the problem with .net core 3 updated. they updated the routing in razor pages in .net core 3.
ASP.NET Core 3.0 added many new features but, as far as I know, conceptually ASP.NET Core Razor Page routing works the same as it did in previous versions. Endpoint routing is a new enhancement where routing is a middleware service but should not affect
how routes resolve.
I tried this, its not updated to .net core 3 i think. my question have the code from the link you suggested. its the problem with .net core 3 updated. they updated the routing in razor pages in .net core 3.
Routing in Razor Pages is based on building attribute routes from the file paths of razor pages. Nothing was changed in that area in .NET Core 3.
What you need to do, if you still need help, is to show the configuration you have put in your startup class that's relevant to routing, then show the @page directive for the relevant page including any route template, provide details of its file location
and explain what URL you hope you find the relevant page at.
Member
2 Points
18 Posts
How to create seo friendly url with category in Razor Pages Asp.Net Core 3
Nov 12, 2019 05:56 PM|mejanh|LINK
Looks like they update Razor Pages routing in .Net Core 3. I tried with the old one like this code below
its showing a blank Page instead of Home/Index.csml file.
Question-1: How can i change the default folder in Razor Pages?
Now, I created a create post form with slug input. that means i have slug and CategoryID stored in Db where Post have relationship with category model with CagoryID and Category type Model. My Post url looks like localhost:***/Home/Post?id=4 or /Home/Post/4. I have used asp-page and asp-route-id tag helpers to do that.
Question-2: What i want is Localhost***/CategoryName/slug
My Post.cshtml.cs get code:
It would be really helpful for me. Please help me.
All-Star
194428 Points
28074 Posts
Moderator
Re: How to create seo friendly url with category in Razor Pages Asp.Net Core 3
Nov 12, 2019 10:32 PM|Mikesdotnetting|LINK
The root directory name doesn't form part of the URL. Pages are rooted in the RootDirectory. Your Home/Index.cshtml page is accessible at / or /Index. If you want to have a url that includes Home, remove the RootDirectory configuration, add a Home folder to Pages, and then add your pages there.
Member
2 Points
18 Posts
Re: How to create seo friendly url with category in Razor Pages Asp.Net Core 3
Nov 13, 2019 09:34 AM|mejanh|LINK
What i added in the sample code was Home folder and there is index page as there as well.
All-Star
194428 Points
28074 Posts
Moderator
Re: How to create seo friendly url with category in Razor Pages Asp.Net Core 3
Nov 14, 2019 06:55 AM|Mikesdotnetting|LINK
https://www.learnrazorpages.com/razor-pages/routing
Member
2 Points
18 Posts
Re: How to create seo friendly url with category in Razor Pages Asp.Net Core 3
Nov 14, 2019 08:28 AM|mejanh|LINK
i tried the Learnrazorpages website methods. its not working. and i think that instructions are for 2.2. please read my question again. after adding Home to root directory the whole web app shows blank page.
All-Star
194428 Points
28074 Posts
Moderator
Re: How to create seo friendly url with category in Razor Pages Asp.Net Core 3
Nov 15, 2019 07:32 AM|Mikesdotnetting|LINK
What URL are you trying to reach the home page at?
Member
2 Points
18 Posts
Re: How to create seo friendly url with category in Razor Pages Asp.Net Core 3
Nov 15, 2019 08:35 AM|mejanh|LINK
Please read the question. Its in the code that i tried to change it to /Home directory.
All-Star
53001 Points
23587 Posts
Re: How to create seo friendly url with category in Razor Pages Asp.Net Core 3
Nov 15, 2019 12:27 PM|mgebhard|LINK
You misunderstand the RootDirectory configuration. RootDirectory maps a relative directory to the application root. The RootDirectory is not shown in the URL because it is the application root. For example, the user goes to http://www.myapp.com. The framework returns /Pages/Index.cshtml.
Reference documentation.
https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.razorpages.razorpagesoptions.rootdirectory?view=aspnetcore-3.0
Member
2 Points
18 Posts
Re: How to create seo friendly url with category in Razor Pages Asp.Net Core 3
Nov 15, 2019 12:33 PM|mejanh|LINK
Then what is the solution please?
All-Star
53001 Points
23587 Posts
Re: How to create seo friendly url with category in Razor Pages Asp.Net Core 3
Nov 15, 2019 01:00 PM|mgebhard|LINK
Solution to what? How to implement SEO URLs? Can you explain the SEO URL design approach?
Member
2 Points
18 Posts
Re: How to create seo friendly url with category in Razor Pages Asp.Net Core 3
Nov 15, 2019 01:16 PM|mejanh|LINK
In Pages Folder I created a Home Folder which i want to make it default folder to show in home link like myapp.com. and for single posts(/Home/Post.cshtml) and category(/Home/Category.cshtml) pages, i want to change urls from myapp.com/Post?id=1 to myapp.com/technology/how-to-buy-a-mobile and myapp.com/Category?id=1 to myapp.com/Technology. I have slug and category in the post database as i described in the question. so, i don't want to convert post title into slug. i just want to change the url.
My Single Post cs code:
i can add string slug in ongetasync class. i tried also. the only problem here is routing. and i can't find a solution because they updated routing in .net core 3.
All-Star
53001 Points
23587 Posts
Re: How to create seo friendly url with category in Razor Pages Asp.Net Core 3
Nov 15, 2019 02:44 PM|mgebhard|LINK
I recommend that you set aside time to learn Razor Page routing. It's a bit different than MVC. mikesdotnetting, the original responder to your question, created a wonderful guide which was linked above.
https://www.learnrazorpages.com/razor-pages/routing
I recommend going through the link as it contains answers to your questions.
Member
2 Points
18 Posts
Re: How to create seo friendly url with category in Razor Pages Asp.Net Core 3
Nov 15, 2019 02:52 PM|mejanh|LINK
I tried this, its not updated to .net core 3 i think. my question have the code from the link you suggested. its the problem with .net core 3 updated. they updated the routing in razor pages in .net core 3.
All-Star
53001 Points
23587 Posts
Re: How to create seo friendly url with category in Razor Pages Asp.Net Core 3
Nov 15, 2019 03:15 PM|mgebhard|LINK
ASP.NET Core 3.0 added many new features but, as far as I know, conceptually ASP.NET Core Razor Page routing works the same as it did in previous versions. Endpoint routing is a new enhancement where routing is a middleware service but should not affect how routes resolve.
Reference doc
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/routing?view=aspnetcore-3.0#routing-basics
Is there anyway you can provide a code sample that clearly illustrates how Razor Pages routing is not working as expected?
All-Star
194428 Points
28074 Posts
Moderator
Re: How to create seo friendly url with category in Razor Pages Asp.Net Core 3
Nov 16, 2019 04:45 PM|Mikesdotnetting|LINK
What you need to do, if you still need help, is to show the configuration you have put in your startup class that's relevant to routing, then show the @page directive for the relevant page including any route template, provide details of its file location and explain what URL you hope you find the relevant page at.