Within an MVC5 app we have introduced a security model which will authorise the user logged in against the requested Url. I have been looking at ways to auto generate a list of available Urls within the application on start up.
I first entertained the idea of using reflection to build up relative Urls from the area/controller/action names similar to this post
This will work if all the urls match the standard convention within MVC however; we have some routes that do not match this convention so an auto scan using reflection will not cover all grouds.
Is there a way (on application_start event) I can possibly use the route table to determine what Urls are available?
Note:
I understand the Http Context is not yet available at this point but I do not need the whole Url, only the relative url after the domain.
Related
Not having done much with MVC (lots and lots of ASP.NET Applications though) and Blazor, I'm fighting to get my head around how to accomplish what I want in Blazor.
In ASP.NET I would use the NuGet package for FriendlyUrls to accomplish this task.
I want to be able to define a list of URLs or generate them on the fly and have them route to a page I already created.
For example, I might have www.sitename.com/MyArticleName and I want it route to www.sitename.com/articles/1/ behind the scene. So the URL would look like /MyArticleName but in reality it's rendering the /articles page with a fixed ID.
My table in the DB is basically (a bit more to it though) is URL, Rewrite URL.
I can define the routes up front in some cases but when it comes to things like the articles , I would rather not define all the URLs up front as the user can add/rename/delete articles at any time.
Hopefully that makes sense. I know I can do a page like #page "/articles/{articlename}", but I don't want the user to see the /articles part of the URL.
I have an umbraco installation.
I published a content named "Account" and umbraco gave it this url /account.
Fair.
Then I published a content named "Register" under content Account (I allowed Account Doc Type to have Register Doc Type as child).
I would expect the url of the new content to be /account/register, but umbrace gave it /register.
Why is this happening? What's the point of allowing child content types, and building a content tree, if the urls are all from root? As a newbie to umbraco, I consider it a logic flaw. For a CMS that claims to be friendly, that's not friendly at all. I'm a developer but I can't waste half of my day looking for umbraco answers and tutorials online. A friendly CMS should be self explicatory while being used, and should take care of all common assumptions a newbie may have.
How can I make the Account->Register content node to appear in /account/register url?
I think what you're seeing is a side-effect of a feature in Umbraco that is supposed to support the pattern most develops use when creating an Umbraco website.
The best practice when building Umbraco websites is to create a "Website/Root/Site" document type and place this in the root of your hierarchical content structure. Beneath this document type you place each of your pages as direct children. This allows you to set hostnames and culture on your site as well as it allows you to keep your entire site in one "bundle", and it also allows you to do multiple subsites within the same Umbraco website. It is pretty common practice to structure your website this way.
However - as you expect - this would give your URLs such as domain.com/website/account/register due to Website now being an actual content node in Umbraco. As this is really not something anyone would want - Umbraco has a built-in feature allowing the top level node to be hidden from the URL path. This results in your URLs being domain.com/account/register in this case.
If you however consider your site - this feature results in your register page getting the URL: /register since the /account part is a top level node that will be ignored when generating URLs.
This behavior is triggered by the setting umbracoHideTopLevelNodeFromPath which can be found in web.config of your site (true by default).
I would however recommend that you do not change this setting and instead add in the root node for your site as it will make things easier for you to manage in the future - it is also the best practice way to structure a site.
This should result in URLs being generated the way you are expecting them to be. Child nodes will have URLs that reflect their name and location in the content tree.
I would like to make my app url like http://domain.com/product/product-name....
Is it possible that is if I am passing productid and can get productname in my url?
URL Rewriting is the practice of "faking" a URL by rewriting it to look like another URL. It does so in a single direction. This means it is has no built-in way to generate a URL for use on the UI.
.NET Routing is what MVC applications typically use. It is a real, not faked 2-way mapping from URL to a set of route values and/or a set of route values to a URL. This means that you can easily generate URLs for use within the application using HTML helpers such as ActionLink, and as such is the recommended and preferred approach for building custom URLs from within the application.
Sometimes both of these techniques are used, but URL rewriting is generally only put in place to do things that make sense to change from outside of the compiled application, such as when nesting the application within another application or putting in one-way 301 redirects for legacy URLs.
To answer your question, yes it can be done, but should be done with .NET routing, not URL rewriting. To use names like this, you need a mapping between name and ID. A couple of ways this could be done:
Make a Name-ID mapping right in your route configuration by using static route segments with the name and route values that contain the related ID. This approach will work only if your URLs don't need to be changed dynamically within the application.
Use a Name-ID map within a cached dictionary object. Use a custom RouteBase implementation to do mapping in each direction by overriding the GetRouteData and GetVirtualPath methods.
I am currently working with localizing my MVC4 web application and have run in to an issue. My site is in four languages, English, French, Russian and Polish. I set up the sites culture based upon what the user selects. This is not tracked in the url.
I want my urls to be SEO friendly. So I need them to be localized. The tricky part though is that fact I routes set up for my controllers in English. For example
/product/product-name is mapped to the product controller and the get action
/customer/details is mapped to the customer controller and the details action
How can I localize the routes to different languages/cultures? So when I create an action link it maps to correct controller/action, generating the localized url?
I found this solution and it is very elegant but the issue I have is that it does not work as well for dynamic url's, as there is a need for explicit mapping.
Actually I don't know the right answer for that question and it's really very interesting how it possible to do that with standard MVC routing.
But if you do not find proper way to implement that with standard options you could implementing that by hands with the helps of front controler.
You create the class which will be main point of availability inside your app. And inside that method you could implement redirecting logic which will redirect you to the proper controller/action.
I have a web app, A, that has a virtual directory that is also an app, B. I've tried to create an MVC route in A such that the URL appears to be in the virtual directory but is still handled by A and not B. However, it seems to be being ignored. Is there a way of making the MVC route take priority over the virtual directory?
If you want App A to override App B, you're going to have to add some Url Rewriting configuration, not MVC routing logic, which, if you're using IIS7+ which, I believe, you should be set up in the web.config file for App A. IIS has to know which application to forward a request to; by the time MVC routing is invoked, the application has already been selected at that point (you cannot unselect B once the actual application is invoked * ). You also might be able to get away with setting up the url rewrite rules in App B to point to App A, but I'm not quite sure how that'd work.
Please note, what I'm saying about application invocation is very likely not strictly true. But to be honest, I don't know if I could give a completely accurate description of the lifecycle of a url rewritten request; I just hope I'm roughly approximating what happens in simplified terms. Just learn more about url rewriting in IIS and IIS7 request lifecycles visit the following links:
http://www.iis.net/learn/extensions/url-rewrite-module/using-the-url-rewrite-module
http://msdn.microsoft.com/en-us/library/bb470252(v=vs.100).aspx
So you got two apps A & B. You say you tried to create MVC route but in which app? If you can explain a bit more what you trying to achieve, it will be easier to answer.
ASP.net routes (also available in web forms) can take any form you want. The one thing you must keep in mind that the route maps are greedy. So if most generic route is found first it is not going to check anything below no matter what. So the order should be most specific and last one should be most general.
Check this post http://yogiorchard.azurewebsites.net/archiving-items-and-a-routing-lesson
Hope this helps