How to exclude certain paths from routing in ASP .NET Core - c#

I have a project where all paths are routed through the same /index page, however this directs images/css/js files through the same page. In the example below, I am wondering how would I exclude those resources (i.e. /images/, /js/, /css/*, etc) from being routed to the /index page?
services.AddRazorPages().AddRazorPagesOptions(options =>
{
options.Conventions.AddPageRoute("/index", "{*url}");
});

Using this regex, I was able to route everything except if the path has "images/" and it seems to work:
options.Conventions.AddPageRoute("/Index", "{*url:regex(^(?!images/).*$)}");

Related

Is there a way in a Razor Pages app to change the default starting route to an Area?

When you create an ASP.Net Core Web App with Individual Accounts it adds an Identity Area along with the regular Pages folder.
Since I don't want to be working between the Pages folder along with the Area folder, I want to create a Home Area that my app's default route "/" will route to.
I was able to accomplish this for the Index page by adding this to Program.cs:
builder.Services.AddRazorPages(options =>
{
options.Conventions.AddAreaPageRoute("Home", "/Index", "");
});
Obviously, this just takes care of the single Index page, but if I wanted to have an About or Privacy page, I would have to add AreaPageRoutes for them as well. I tried a couple different things with AddAreaFolderRouteModelConvention that I am too embarrassed to show here, but was ultimately unable to find out how to make it work.
Is it possible and secondly, is it bad practice, to map the default routing of Pages to a specific Area?
You can use a PageRouteModelConvention to change the attribute route for pages in an area:
public class CustomPageRouteModelConvention : IPageRouteModelConvention
{
public void Apply(PageRouteModel model)
{
foreach (var selector in model.Selectors.ToList())
{
selector.AttributeRouteModel.Template = selector.AttributeRouteModel.Template.Replace("Home/", "");
}
}
}
Generally, my advice regarding areas in Razor Page applications is don't use them. They add additional complexity for no real gain at all. You have already found yourself fighting against the framework as soon as you introduced your own area. QED.
The only place areas serve a real purpose is within Razor class libraries, so you have to live with an area when using the Identity UI. But you don't have to use the Identity UI. You can take code inspiration from it and implement your own version in your existing Pages folder.

Route to generic Razor page only if physical resource doesn't exist

I am a front-end dev and don't know a lot about C#. I have been dabbling in Razor views so I can sort-of make my way around it. I am developing a project that might require some C# routing and I can't get it working (I think this is due to my limited understanding).
We built a website that contains a mix of static resources (physical .cshtml pages) and then also content that is stored in a database. All the links to these content pieces are generated via handlebars templates so I cannot use Razor within my templates (that I know of). The database content needs to use page.cshtml, which should then also be routed to remove page from the URL.
So in essence, when you hit http://www.example.com/my-page it should check if my-page.cshtml exists, else use http://www.example.com/page/my-page rewritten as http://www.example.com/my-page.
I'm trying to use this but it isn't working:
using System.Web.Routing;
public class Routes
{
void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("Deals",
"/{page}", "~/page.cshtml");
}
}
Is this possible using only Razor? I have tried routing via C# RouteConfig but the project is using V4 of .NET so attribute routing doesn't work. I also can't use a catchall route as there are some pages that are within directories.
Or is my only option to explicitly state a link to /page/ in the DB and do IIS rewrites?
My other option is to always hit page.cshtml and if it can't find the content in my DB, it loads in my-page.cshtml as a partial. But then what happens if someone types http://www.example.com/my-page into the address bar?
You can check if your my-page.cshtml file exist in your controller
string myFile = Server.MapPath("path to my-page.cshtml");
if(File.Exists(myFile))
{
return View("path to my-page.cshtml");
}
else
{
//from db
}
ps: you need to use System.IO in your controller

Routing Issue want to display domain name even index.aspx is called

routes.MapPageRoute("Main", "", "~/index.aspx");
thats the route I mapped on index page..
when I call the url with index.aspx it displays like
www.abc.com/index.aspx
but I want it to show
www.abc.com
even when index.aspx is called
Regarding my comment, URL Rewrite is also available in IIS and Asp.net. So you can potentially use it.
Another solution would be to redirect to your route. The route alone doesn't change the URL, it just allows you to access the resource via the defined route.
You can redirect to the route though, which will rewrite the URL on the client
For example like this:
if (Request.Path != "/")
{
Context.Response.RedirectToRoute("Main");
}
This is very simplified and might not work in all scenarios, so please be very careful.

URL routing turning /pages/test.aspx into /test

I made a folder called 'pages' in my asp web forms project. In this folder I have a lot of pages:
test.aspx
hello.aspx
When I open these pages in a browser I get:
www.domain.com/pages/test.aspx
www.domain.com/pages/hello.aspx
This is normal, I know. But what if I want to delete the /pages in the url and just show (without .aspx):
www.domain.com/test*.aspx*
www.domain.com/hello*.aspx*
I can do this by manually adding a new route (in RegisterRoutes() method) for each page but is there a way to do this dynamicly?
I found this question but I don't know if I can use it for this problem.
WebForms custom / dynamic routing
Try something like this, not sure if that will do it. But you could always just put your pages in the root directory. I think this would work.
Source Link: http://msdn.microsoft.com/en-us/library/vstudio/cc668177(v=vs.100).aspx
routes.MapPageRoute("PageRoute","{page}", "~/pages/{page}.aspx");
Not sure if that's what you were looking for, this way you can just do something like:
Response.RedirectToRoute("PageRoute", new { page = "test" });

route.MapPageRoute to a page with an extension

In the global.asax of my website project (not MVC, not Web Application) MapPageRoute won't let me map to a page with an extension.
For example:
routes.MapPageRoute("GetMobilePassForAttendee", "Pass/Attendee/{attendeeId}", "~/GetMobilePass.aspx");
works as expected, but
routes.MapPageRoute("GetMobilePassForAttendee", "Pass/Attendee/{attendeeId}/pass.pkpass", "~/GetMobilePass.aspx");
returns a 404.
Anyone know why?
Perhaps I should be using URL rewriting, but all the material I've read has suggested I use routing instead.
Have you tested the "pass.pkpass" page separately, let say attendeeid=1 to see if Pass/Attendee/1/pass.pkpass is a working page without the routing?
Because .NET routing would not work in this case if this is a working page. It will skip the routing and load the actual "Pass/Attendee/1/pass.pkpass" page.
This worked for me:
routes.MapPageRoute("FederationMetadataRoute", "FederationMetadata/2007-06/{file}", "~/FederationMetadata/2007-06/FederationMetadata.aspx")
And then I just check the {file} parameter to make sure it equaled FederationMetadata.xml
So in your case, just set Pass/Attendee/{attendeeId}/pass.pkpass to Pass/Attendee/{attendeeId}/{pkpass}
and check {pkpass} to make sure it equals pass.pkpass on your page.

Categories

Resources