3

I need to change the URL on my address bar.

I looked for URL Rewrite but as far as I've seen it works for a request like this:

url.com/mypage.aspx?xp=asd&yp=okasd

and transforms that into:

url.com/mypage/asd/okasd

http://www.iis.net/downloads/microsoft/url-rewrite

That's not my scope. I already have MVC Routes working with url.com/mypage. The problem is that when I type that URL I am redirected (that's the behavior I want) to url.com/otherpage/actionX?param=value. The problem is that I still want the address bar to show url.com/mypage. Will URL Rewrite work for that? I am asking because I don't know if it will work since it's an internal redirect (RedirectToAction) instead of a 'regular' access. In case someone wonders why I can't make a route for that, as explained in my question I alread have one rule for that url.com/mypage that redirects to a 'router' which decides what action to call.

I've seen some questions, but I don't think they cover my specific problem:

MVC3 change the url

C# - How to Rewrite a URL in MVC3

UPDATE

This is my route:

routes.MapRoute(
    "Profile", // Route name
    "{urlParam}", // URL with parameters
    new { controller = "Profile", action = "Router" } // Parameter defaults
);

Inside Router action I redirect to the correct action according to some validation done on urlParam. I need this behavior since each action returns a different View.

Updated my tags since I am now using MVC4

Thanks.

3
  • Take a look into this one stackoverflow.com/questions/799511/… Commented Apr 3, 2013 at 13:00
  • @ConradClark That seems interesting, I'll take a look if that'll work in my case. Thanks. Commented Apr 3, 2013 at 14:29
  • Has it worked for you? If so, pls mark the answer. Commented May 15, 2013 at 9:27

4 Answers 4

0

I once had to run a php site on a windows box. On the linux box it originally ran, it had a rewrite defined to make site process all request in only one php file (index.php).

I've installed and configured URL Rewrite with following parameters

Name              : all to index.php
Match URL ------------------------------
 Requested URL    : Matches the Pattern
 Using            : Regular Expressions
 Pattern          : (.*)
 Ignore Case      : Checked
Conditions -----------------------------
 Logical Grouping : Match All
    Input         : {REQUEST_FILENAME}
    Type          : Is Not a File
Action ---------------------------------
 Action Type      : Rewrite
 Action Properties:
   Rewrite Url         : /index.php?$1
   Append Query String : Checked
   Log Rewritten URL   : Checked

this makes all requests to site (except files like css and js files) to be processed by index.php

so url.com/user/1 is processed on server side as url.com/index.php?/user/1 since it works on server side client url stays same.

using this as you base you can build a rewrite (not a redirect).

Sign up to request clarification or add additional context in comments.

2 Comments

The only problem is that MVC already uses this rule url.com/Controller/Action/ and I already have a mapped route to handle url.com/userInput. But I'll implement your solution and see if it works in my case, thanks.
then, good thing that IIS Rewrite modifies the request before it is even processed by asp.net runtime. all asp.net will see is the rewritten url. please see iis.net/learn/extensions/url-rewrite-module/… for details
0

Server.Transfer is exactly what you need, but that is not available on MVC.

On the MVC world you can use the TransferResult class defined in this thread.

With that... you add code to your ROUTE action that process the urlParam as always and instead of "redirecting" (RedirectToAction) the user to a new URL, you just "transfer" him/her to a new action method without changing the URL.

But there it a catch (I think, I have not tested it)... if that new page postbacks something... it will NOT use your router's action URL (url.com/mypage), but the real ACTION (url.com/otherpage)

Hope it helps.

1 Comment

I have already tried that approach as suggested by @Conrad Clark but it didn't work as well.
0

In my opinion,you can try following things:

  1. Return EmptyResult or RedirectResult from your Action method.
  2. Also,you need to setup and construct outbound route for URL that you required.

Secondly, if these didn't work,the crude way to handle this situation is with Div tag and replacing the contents of Div with whatever HTML emitted by Action method. I am assuming here that in your problem context, you can call up jquery ajax call.

Hope this Helps.

2 Comments

I will try this in a few hours and let you know. Thanks for the answer.
I'm sorry for the delay, but no, it did not work. I decided to create a different action for a while. Until I find an answer. Thanks either way.
-1

The problem you have is that you redirect the user ( using 302 http code) to a new location, so browser ,reloads the page. you need to modify the routes to point directly to your controller. The route registration should be routes.MapRoute("specific", "my page", new { controller = "otherpage", action="actions", param="value"}). This route should be registered first

2 Comments

Thank you, but as I said I need to access one action when url.com/mypage is accessed and then that action will see to what action it will redirect, did you understand? thank you
"redirect" is as i said already, will always show a new URL. What you can do to have the same URL,you can craete a view for url.com/mypage and inside the view call html.RenderAction("NewAction", "NewController") but you need to make shure that ("NewAction", "NewController") will return a partial view

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.