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.

Community
  • 1
  • 1
eestein
  • 4,914
  • 8
  • 54
  • 93

4 Answers4

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).

Erdogan Kurtur
  • 3,630
  • 21
  • 39
  • 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. – eestein May 07 '13 at 16:47
  • 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 http://www.iis.net/learn/extensions/url-rewrite-module/iis-url-rewriting-and-aspnet-routing for details – Erdogan Kurtur May 07 '13 at 20:57
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.

Community
  • 1
  • 1
Daniel
  • 1,424
  • 1
  • 12
  • 19
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.

  • I will try this in a few hours and let you know. Thanks for the answer. – eestein May 09 '13 at 11:54
  • 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. – eestein May 15 '13 at 10:31
-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

Alexandr Mihalciuc
  • 2,537
  • 15
  • 12
  • 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 – eestein Apr 03 '13 at 12:35
  • "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 – Alexandr Mihalciuc Apr 03 '13 at 12:58