Can we add dynamically routes to global.asax file
Suppose if I have multiple routes for the same page for example
http://website.com/about
http://website.com/en/about
http://website.com/en/about-us
While my actual URL for the page is like http://website.com/en/about-us.
My question now is: is there a way I can dynamically define these routes in global.asax file in such a way that it reads the URL entered by users like http://website.com/about and then compares it with database table and redirects it to the correct page which is http://website.com/en/about-us?
Taking into consideration following Table Structure:
Id URL_Name URL Actual_URL Page_Handler
1 Home http://website.com/ http://website.com/ Default.aspx
2 About Us http://website.com/about http://website.com/en/about-us About.aspx
3 About Us http://website.com/about-us http://website.com/en/about-us About.aspx
4 About Us http://website.com/en/about http://website.com/en/about-us About.aspx
5 Contact http://website.com/contact http://website.com/en/contact-us Contact.aspx
6 Contact http://website.com/en/contact http://website.com/en/contact-us Contact.aspx
Right now I have to configure each route manually in the global.asax:
if(HttpContext.Current.Request.Url.ToString().ToLower().Equals("http://website.com/about")
{
HttpContext.Current.Response.Status = "301 Moved Permanently";
HttpContext.Current.Response.Redirect("http://website.com/en/about-us");
}
if(HttpContext.Current.Request.Url.ToString().ToLower().Equals("http://website.com/en/about")
{
HttpContext.Current.Response.Status = "301 Moved Permanently";
HttpContext.Current.Response.Redirect("http://website.com/en/about-us");
}
A pointer to a good example or a solution is highly appreciated.
I find routes in global.asax are great for static resources especially if you want a nice, sexy extensionless URLs for SEO.
For dynamic pages/URLs though, I tend to have a catch all route that handles the request if it doesn't match any static routes.
eg
// ignore
routes.Add(new System.Web.Routing.Route("{resource}.axd/{*pathInfo}", new System.Web.Routing.StopRoutingHandler()));
// sexy static routes
routes.MapPageRoute("some-page", "some-sexy-url", "~/some/rubbish/path/page.aspx", true);
// catch all route
routes.MapPageRoute(
"All Pages",
"{*RequestedPage}",
"~/AssemblerPage.aspx",
true,
new System.Web.Routing.RouteValueDictionary { { "RequestedPage", "home" } }
);
So, when a request comes in it checks each static route in turn and executes the specified page. If no match is found, it drops through to the catch all and then AssemblerPage.aspx handles the request. This AssemblerPage will analyse the requested URL and redirect, rewrite path or stick some controls on the page to render - basically, it can do whatever you want it to do.
In your case, I'd have the AssemblerPage check the DB and compare the requested URL with the URLs in your table. Then simply redirect or rewrite path.
Would ASP.NET Routing help you here ?
Have a look at this: - ASP.net URL rewrite based off query string ID
Related
i have a two pages Home.cshtml and Job.cshtml
in Startup file i mange this
services.AddRazorPages().AddRazorPagesOptions(options =>
{
options.Conventions.AddPageRoute("/Home", "/{handler?}");
});
services.AddRazorPages().AddRazorPagesOptions(options =>
{
options.Conventions.AddPageRoute("/Job", "{*url}");
});
i want to open this url for Home.cshtml
http://localhost:44004/au/hospital-jobs-in-India
and second url is i want to open this in Job.cshtml
http://localhost:44004/best-jobs-in-India/Page-3/NewsData
Note: Now my issue is that for both url it hit the Job.cshtml
but i want to hit for separate pages.
You should understand what is the friendly routes in Razor pages . As your current route template , /au and /au/ will map to Home page it app catches any URL that doesn't map to a physical file , and /au/hospital-jobs-in-India will map to Job page since the route doesn't match the first friendly route , but it match the {*url} route template . You should modify the route based on your requirement , for example ,change the first route template like /au/{handler?} .
In addition , you don't need to register AddRazorPages multi times , you can directly add multiple AddPageRoute inside one AddRazorPagesOptions .
I am working on an ASP.NET MVC 5 website that has a public "marketing" website that contains somewhat static pages with information about the company, legal, social, contact us, etc. that a non-logged in user can access. Then, once logged in, there is a back end of the website that registered users have access to features.
Originally I had all the public "marketing" pages going to http://www.mywebsite.com/Marketing/About, http://www.mywebsite.com/Marketing/Social, etc.
However, the business wants all the "marketing" pages, to be accessible a single directory down from the root website, so the links above would be accessible with:
http://www.mywebsite.com/About, http://www.mywebsite.com/Social, etc.
I know I can use the below approach to get it to work by registering individual routes for each "marketing" page like so:
routes.MapRoute(
"ShortAbout",
"About",
new { controller = "Marketing", action = "About" }
);
routes.MapRoute(
"ShortSocial",
"Social",
new { controller = "Marketing", action = "Social" }
);
However, since there are about 15 "marketing" pages, this seems inefficient and it seems like there must be a better way to do this.
I also tried a generic routing approach outlined here: http://www.wduffy.co.uk/blog/aspnet-mvc-root-urls-with-generic-routing/
but the problem with that approach was I had a "marketing" page, with the same name as a controller and it ended up forwarding the user to the marketing subdirectory. For example, I had a Controller called "MachineController", and in the "MarketingController" I had an action/page called "Machine", so it was forwarding the user to /Marketing/Machine using the approach in the above link.
Any other ideas? Thanks in advance.
I had exactly this problem. A much simpler but more hardcoded solution is
routes.MapRoute("MarketingPages", "{action}",
new { controller = "Marketing" },
new { action = #"About|Social" });
The last anonymous object constrains the route to match routes where action matches the supplied regular expression, which is simply a list of the urls you want to have marketing pages. Any other url like '/something' falls through to the routes below.
I would like my ASP.NET MVC4 application to only serve the base HTML markup for a specific page, and after that I'm processing everything else on client-side with knockout.js/history.js/AJAX, including the initial page load.
So when someone refers to URL http://example.com/products/list/food/fruits, the MVC router should simply ignore everything what is behind "products/list" and route the request to ProductsController and List action. Then on client-side I will handle the rest and load the requested data accordingly.
I was playing with the route definitions, I tried to completely skip the "products/list" route, I also tried to add a "products/list/*" route, but didn't have success yet.
You can use an asterisk as part of the last variable in a route. For example, when configuring your routes:
routes.MapRoute(
"ProductRoute",
"products/list/{*otherArgs}",
new { controller = "Products", action = "List" });
You can learn more in MSDN's Documentation on routing under the section "Handling a Variable Number of Segments in a URL Pattern"
You will need to create your own route.
Something like this should do the trick:
routes.MapRoute("Products", "Products/{List}",
new {controller = "Products", action = "List"}
);
Note: I´m not sure if the other parameters are required in the route.
This app has several routes configured in RouteConfig.cs. For instance, I have the two following routes defined:
routes.MapRoute(
name: "MyPage-Demo",
url: "pages/page-title/demo",
defaults: new { controller = "Root", action = "PageDemo" }
);
routes.MapRoute(
name: "MyPage",
url: "pages/page-title/{resource}",
defaults: new { controller = "Root", action = "Page", resource = UrlParameter.Optional }
);
Each page someone visits has a link to a "demo". A page could be accessed by visiting http://localhost/pages/page-title. This works fine.
When a user clicks the "demo" link, they are redirected to a page located at http://localhost/pages/page-title/demo. This works fine.
My problem is the demo page may reference a complex nested structure. The structure consists of JavaScript, css, images, etc. Content used for the purpose of the demo. None of these nested resources can be found. However, I'm not sure how to setup my routing to account for these nested files.
I'm confident I'm going to need to update my controller's PageDemo action. However, I'm not sure
a) how to do so in a way that will allow for differing structures and
b) how to update my route configuration to account for these nested structures.
Is there a way to do this? In reality, I'm going to have multiple pages and multiple demos. For that reason, I want to have something a little more reusable than a hard-coded approach.
If you just need to serve files physically stored in a path, you should be able to just ignore the route, e.g.:
routes.IgnoreRoute("pages/page-title/demo/resources/{*resource}");
That will bypass MVC trying to route the request to a controller.
Or you could go by file extension:
routes.IgnoreRoute("{file}.js");
routes.IgnoreRoute("{file}.css");
(Code is untested, but it looks like you're trying to do something similar here :)
https://stackoverflow.com/a/3112192/486620
IF I understand:
The problem seems to be that your MyPage-Demo route:
routes.MapRoute(
name: "MyPage-Demo",
url: "pages/page-title/demo",
defaults: new { controller = "Root", action = "PageDemo" }
);
is NOT {resource} specific, while your MyPage route IS.
If you change your route to take a {resource}
routes.MapRoute(
name: "MyPage-Demo",
url: "pages/page-title/demo/{resource}",
defaults: new { controller = "Root",
action = "PageDemo", resource = UrlParameter.Optional });
Then your action method can
return specific Views with proper resource settings
set a Viewbag property with path to your specific resource
If this is inline with your intent, these routes can be consolidated into
routes.MapRoute(
name: "MyPage-Demo",
url: "pages/{action}/{resource}",
defaults: new { controller = "Root",
action = "PageDemo", resource = UrlParameter.Optional });
/pages/PageDemo/{resource} resolves to Controller=pages, action = PageDemo
/pages/demo/{resource} resolves to Controller=pages, action = demo.
This convention allows you flexibility to create more {resource} dependant links
In the Browser, Right Click Demo page => Choose View Page Source.
Here, you have the link for the CSS and Js files in your Demo page. Click on those js/css file links. Check if there are redirecting you to the correct/expected location. Otherwise you could make the Css/Js file URL accordingly Because, as per the demo page each PageDemo will have its own unique structure of JS/Images/css, etc
How are you referencing your JS and CSS files ?
If you use the tilde character like : ~/Content/Styles/Site.css you won't have any problem no matter where you are in your virtual path.
Also not 100% sure I am directly answering your question, but making the assumption that the resources you are trying to access are nested in a folder structure that mirrors the page structure - and the issue you are having is how to ignore the routes to these without having to know what they might be in advance?
This does a good job of explaining that: https://stackoverflow.com/a/30551/1803682
I would ask:
As #PKKG notes in his answer - do the links in the page source match what you expect?
How is this per-demo content served: e.g. by a service and not a static file?
this answer contains two approaches. the second one may be more suitable for your scenario. the first may be more suitable for a general mvc project
approach one
i suggest creating a organized structure in your content folder to store the scripts and css files, ie
/Content/Demos/Page-Title-1/
/Content/Demos/Page-Title-2/
/Content/Demos/Page-Title-3/
and
/Content/Demos/Common/
and then make a bundle to render the scripts and css files for each page title
ie.
bundles.Add(new StyleBundle("~/Demo/page-title/css").Include(
"~/Content/Demos/Page-Title-1/csscontent1.css",
"~/Content/Demos/Page-Title-1/csscontent2.css",
"~/Content/Demos/Page-Title-1/csscontent3.css",
"~/Content/Demos/Page-Title-1/csscontent4.css"));
bundles.Add(new StyleBundle("~/Demo/page-title/js").Include(
"~/Content/Demos/Page-Title-1/jscontent1.css",
"~/Content/Demos/Page-Title-1/jscontent2.css",
"~/Content/Demos/Page-Title-1/jscontent3.css",
"~/Content/Demos/Page-Title-1/jscontent4.css"));
this will allow you to render the scripts on the demo page using a few line approach, ie.
#Styles.Render("~/Demo/page-title/css");
#Scripts.Render("~/Demo/page-title/jss");
#Styles.Render("~/Demo/common/css");
#Scripts.Render("~/Demo/common/css");
you will have to update the files in global .asax as you change the files in your /Content/Demos/Page-Title/ folder.
there is the benefit that if you choose, you may bundle and minify the files to save bandwidth and load time for the first page load.
approach two.
(still use the following folder structure
/Content/Demos/Common/
and
/Content/Demos/Page-Title-1/
/Content/Demos/Page-Title-2/
/Content/Demos/Page-Title-3/)
make an html helper to reference all the scripts & contents in a folder
its usage would be
#Asset.RenderAssets( '~/folderdirectory')
and the helper would do something like
#helper RenderAssets (stirng directory){
#* scrape the directory for all script files*
var scripts = find all scripts in the directory
#* include the script files *#
for each script
<script src=" ... .js"></script>
#* scrape the directory for all cssfiles*
var styles = all css in the directory
#* include the css files *#
for each style
<link rel='stylesheet' type="text/css" href=" ... .css">
}
this would be a few line usage in each demo view
#Asset.RenderAssets( '~/Content/Demos/Common')
#Asset.RenderAssets( '~/Content/Demos/Page-Title')
you may or may not need to pair this with an extra few line or two in your global.asax or RouteConfig.cs file (see source 3)
routes.IgnoreRoute("/Content/Demos/{page}/{script}.js");
routes.IgnoreRoute("/Content/Demos/{page}/{style}.css");
relevant sources
to create html helpers see
http://weblogs.asp.net/scottgu/archive/2011/05/12/asp-net-mvc-3-and-the-helper-syntax-within-razor.aspx
to use bundling and minifcation (the scripts.render approach) see
http://www.asp.net/mvc/tutorials/mvc-4/bundling-and-minification
phill haakk says may not need to pair this with an ignore route!
https://stackoverflow.com/a/30551/1778606
commentary and edits are encouraged.
All static content (.js, .css, .html, .png) is not seen by MVC (unless modules/runAllManagedModulesForAllRequests is set to true in web.config). Static content extensions are defined in IIS configuration "module mapping", and is using the StaticFileHandler module (and not the .NET module).
So static content must be referenced by its physical path relative to the current path (the path of the current html page).
The best solution is to use absolute link from the root of the website. Like /content/demo1/demo1.html, put all js,css in /content/demo1/, and in demo1.html use path relative to the /content/demo1/ folder (where the .html is). Ie: with demo1.css being in the same folder.
The link to demo1.html would be demo 1
I have an existing site that I'd like to convert to use routing, and after reading Scott Guthrie's post here, I built a working sample that works for most circumstances. However, since not all of the pages on the existing site match a particular pattern, I'll need to check against a database to determine which route (destination .aspx page) to use.
For example, most pages are like this:
http://www.mysite.com/people/person.html
This is fine - I can easily route these to the view_person.aspx page because of the 'people' directory.
But some pages are like this:
http://www.mysite.com/category_page.html
http://www.mysite.com/product_page.html
This necessitates checking the database to see whether to route to the view_category.aspx page or the view_product.aspx page. And this is where I'm stuck. Do I create an IRouteHandler that checks the database and returns the route? Or is there a better way? The only code I've found that kind of fits is the answer to this question.
Thanks in advance.
If you don't mind doing so, the cleanest solution is to:
http://www.mysite.com/pages/category_page.html
In ASP.NET MVC, this situation would be handled a little differently, by specifying a default controller and action method on the root route.
Your route handler doesn't check the database. It sends all the requests to a handler .aspx script. It's that script that checks the database.
My register route looks like...
private static void RegisterRoutes()
{
Route currRoute = new Route("{resource}.axd/{*pathInfo}",
new StopRoutingHandler());
RouteTable.Routes.Add( "IgnoreHandlers", currRoute);
currRoute = new Route("{urlname}",
new EPCRouteHandler("~/Default.aspx"));
currRoute.Defaults = new RouteValueDictionary {{"urlname", "index.html"}};
RouteTable.Routes.Add( "Default", currRoute);
}
The custom handler, which shouldn't be needed in ASP.Net 4.0, simply passes the urlname parameter to the responding script as a URL variable.
Now how often the responding script checks the database depends on how often the data in the database is changed. You can easily cache paths and invalidate the cache when the data is suppose to have changed for instance.
For anyone stuck in the same situation, I ended up adapting the code from this answer to check against a database and return the proper ASPX page.