Remapping a URL to a MVC application - c#

I'm remaking a website, previous build on DotNetNuke. I need to keep the database, as old URLs working.
The issue: translate the old URL
eq.
http://localhost:8069/Noticias/tabid/78/language/pt-BR/nid/2267/Palestra-
discute-o-futuro-profissional-das-crianca.aspx
to the new mapping engine, that would be something like that:
eq.
http://localhost:8069/{controller}/{action}/{id}/{title}
where, in the example, 2267 is my id, and 78 is the module id that i have to 'translate' for a controller name. The action is view by default.
Any suggestions?

Simply make a base page handler for the requests you want to transfer, and then do a simple Response.Redirect to the appropriate URL on your new website then. All your pages inherit these changes, and you can provide a listing of URLs in a configuration file or database for the URLs to dynamically redirect.

Related

MVC RouteConfig vs URL rewrite (in Global.asax)

Myself and a colleague had a problem that we solved in two different ways. But we don't know which is best.
We have a generic MVC page that is populated with specific data (widgets, content, etc) from a database. The user enters a specific URL (user friendly, so trying to keep the query string disguised if we can help it).
Now, the generic page has to take this URL and use it to get the corresponding data from the database to generate a specific page.
Solution 1: In the Global.asax file rewrite the URL, basically creating a Querystring that the generic controller can understand.
Soloution 2: Use the RouteConfig.cs file to force all page requests to route to the generic controller, which then reads the URL.
Any ideas,
Thanks.
RouteConfig.cs is the cleanest way.
If you change your routing configuration, then by using Url.RouteUrl or Url.Action your generated Url's will update along with your routing configuration.

Redirecting url to index.aspx page using standard asp.net3.5 and web.config

I have a subdomain that is http://trade.businessbazaar.in . I am dynamically creating urls from database something in this manner http://trade.businessbazaar.in/mycompany. To display details, I have an index.aspx file there,thinking that on every request the index.aspx page will load and display data accodingly. Also, There is a masterpage on the index.aspx page from where i am capturing the text mycompany and query it in database to fetch result. But nothing seems to work.
A genuine link is http://trade.businessbazaar.in/Symparlife. But its unable to load index.aspx. I need a clean approach without any third party dll or rewriters. Directly to push some lines in config and start working. That is url will be the same but index page will get loaded...
In short, i want to say
I need the StackOverflow type clean url mechanism to fetch pages
Thanks in Advance
You can handle the Begin_Request event in Global.asax and add custom code to redirect to index.aspx and convert the parts of the URL into query string arguments. You should use Server.Transfer to keep the URL in the browser.
I'd recommend upgrading to 4.0 and using the Routing enine though. You should check if the standard routing is available as a download for ASP.NET 3.5. I am sure your code will get messy very soon. Been there, done that.
As #Mike Miller mentions in the comments the Routing engine ships with ASP.NET 3.5. You can check the documentation here - http://msdn.microsoft.com/en-us/library/system.web.routing(v=vs.90).aspx
Here is a tutorial on how to use it with Web Forms - http://weblogs.asp.net/scottgu/archive/2009/10/13/url-routing-with-asp-net-4-web-forms-vs-2010-and-net-4-0-series.aspx
For your case the code would be something like:
routes.MapPageRoute("company-index", "/{company}", "~/index.aspx")
And in index.aspx you can access the route value for company like this:
string company = (string)Page.RouteData.Values["company"];
Keep in mind that you'd better add something in the URL before your actual argument (the company name). If you don't you will have problems later on when because you may want to add a URL like "/Login" but then you will have to validate that users can't create a company named "Login". Not how Stack Overflow has "/questions/" before the actual question info in the URL.

URL shortener using c#

I would like to shorten a url (e.g. http://www.abc.com/products/bag.aspx) to something like (http://short.me/bag). I have found that rules can be added to web.config to detect the short link to open the correct page. But I need a dynamic web.config. Is it a good idea to keep updating the web.config whenever a user creates the short url? Or is there a better way to do it?
I have tried YOURLS, RewriteRule, etc. All don't seems to work properly on my server. I am using a WIN server. I don't really want to use the bitly API as I would like to have my own domain in front of the link. Or is there a way to use the bitly API and still retaining my domain name?
Thank you.
You need to create a database mapping short URLs to long URLs.
You would then create an HTTP handler that looks up the short URL in that database and redirects to the corresponding long URL.
Then, register that handler to run for all requests.
Bit.ly lets you use custom domain names now: https://bitly.com/pro/products, check out this part of their FAQ

Getting text after URL in asp.net / URL Rewriting (sort of!)

My app is a very simple "one page" type app-
It has Default.aspx
I'm basically trying to get, for example:
www.myappurl.com/this is my text
I want to get hold of "this is my text" from the above example.
This will be displayed on the page (for now)
I didn't really want to have to use any complext url rewriting things for this...
(My hosting provider uses IIS6)
I tried using a 404 handler, but this is a bit long winded, and i'm using shared hosting, that can't set the "execute url" on custom 404 pages.
Any other ideas?
You can add a mapping for all requests with the * extension to the ASP.NET isapi dll (GET/POST) verbs. You will need to uncheck the "verify file is on disk" checkbox when mapping the extension in IIS. (In IIS7 integrated mode, you map the extension in the web.config as well). Note that this will caause everything to be served by asp.net, even images and script files, which can slow things down.
Then create a handler mapping in your web.config to a http handler you create.
From there, in the ProcessRequest() method of the handler, you have access to the HttpContext that spawned the request and can manipulate the URL from there.
That is the easiest option, you could also create a HttpModule, or have the default page at root redirect to http://www.domain.com/default.aspx/this is my text, in the code-behind of default.aspx, you will be able to get the text following the page and slash.

Running ASP.NET MVC application behind a proxy with different root relative path

I'm having trouble with paths in a ASP.NET MVC application that's running behind a proxy.
Our IIS Application root path is for example http://server/MyApp/
meaning that all urls using the application root ("~/",Url.Action("MyAction","MyController")) are resolved to "/MyApp"
Now we're running behind a proxy server that forwards all requests, but requires you to access the application through a URL like this:
"/Secury/Proxy/RubbishUrl/MyApp"
Because the proxy url is only available on the client, I thought of creating a cookie with the path prefix, and insert this before each generated URL on the server.
Now the question is, what's the best location in code to modify each URL that's resolved/sent to the client (to resources, controller actions, images etc)?
Every path in the application is resolved with the MVC methods (Url.Content, Url.Action etc).
Update:
Not actively looking for an answer anymore (though still interested in a proper solution)
Most of the time Proxies do their own URL translation, however in this case the proxy server is ignoring paths that are transfered in JSON, and they are processed.
My 'solution' for now is just not passing paths in JSON, but instead:
using proper ID's and values in the JSON requests
creating template in URL's in the HTML (which are resolved properly),
replace the ID's and values in the URL template with the values from the JSON requests
This method is actually a much 'cleaner' way IMO then passing the URL's.
You can create your own asp.net mvc controller factory where to decide which controller and action will serve the response based on the requested url. Check this url for a good blog post on how to do this - http://nayyeri.net/custom-controller-factory-in-asp-net-mvc.

Categories

Resources