0

I try to use URLHelp.RouteUrl with dynamic string in javascript. I want it in this way -

function MyFun(action, param){
   var strPass = "MyController/" + action + "?param1=" + param;
   Windows.Location.href = "<%=Url.RouteUrl(" + strPass + ")%>";
}

It does not work. Have I missed anything? Do we have alternative way in ASP.NET MVC to work in a similar way?

Thanks,

Additional info -

I try the above way because I have a problem from my application - the main page has "http://myserver/myapplicaiton/mycontroller/myaction" correctly return when I use "Windows.Location.href ="MyController/MyAction". But the next page having double controller appeared "http://myserver/myapplication/mycontroller/mycontroller/myaction".
What causes this routing problem?

4 Answers 4

3

This can't work. Razor syntax render's before the html page reaches the browser. Javascript executes after.

So, you are trying to populate step 1 with step 2.

   function MyFun(action, param){          
       Windows.Location ="/MyController/" + action + "?param1=" + param;
    }
Sign up to request clarification or add additional context in comments.

3 Comments

@Don, I looked at your example, and you are using "Windows.Location.href ="MyController/MyAction". That is not the same as "Windows.Location ="/MyController/MyAction" note that adding a leading '/' and removing 'href' should lead the MVC framework to use the web site root
Thanks for your reply. That did not solve my problem. It returns "myserver/mycontroller/myaction" without "/myapplicaiton" after deployed to a server. It works in Visual Studio 2010 but not after deployed.
@Don, are you using any special routes? if so, please include them in your answer. Otherwise, I recommend using a redirect action method to which you will pass the action and controller
2

Try this code instead

   function MyFun(action, param){          
       Windows.Location.href ="/MyController/" + action + "?param1=" + param;
    }

5 Comments

That is what I am using. But this way can not correctly resolve my web site root. I want a way similar to URL.Content("~/") to get a correct path.
try using "~/MyController/" + action + "?param1=" + param
@Don, I admire your urge to follow Best Practice, but you can't dynamically use Url.Content. All Razor syntax is pre-rendered before JS can launch. But, even though it's not best practice, don't fear using simple string path. Dynamic behavior like this requires bending convention a little.
@Don, I think IC, you need a way to resolve relative root. Is the MVC routing engine falling short for you?
@Dave A, You are right. I don't know how to make the routing work. I added information to my question. Thanks
0

You cannot dynamically add razor code to a view once the code leaves the server... Razor is not compiled on the client browser.

You might use jQuery and make an $.ajax() call up to a Controller/Action which has the sole purpose of returning you a url:

public ActionResult GenerateUrlFromRoutingEngine(string ControllerName, string ActionName, string RouteValues) 
{
    var ctx = new HttpContextWrapper(HttpContext.Current);
    UrlHelper helper = new UrlHelper(
                           new RequestContext(ctx,
                           RouteTable.Routes.GetRouteData(ctx));
    var returnUrl = helper.Action(ControllerName,ActionName, RouteValues); //TODO: transform RouteValues to RouteValuesDictionary
    return Content(returnUrl);
}

In the $.ajax callback, set Windows.Location.href = response; (put this javascript on a razor .cshtml file or find a way to pass in the URL intuitively.)

function MyFun(action, param){
    $.ajax({
            type: "POST", 
            url: "@{Html.RouteLink("GenerateUrlFromRoutingEngine", "MyController")}",
            data: { ControllerName: 'MyController', ActionName: action, RouteValues: param },
            dataType: "text",
            success: function(response) { Windows.Location.href = response; }
            }
}

1 Comment

using Ajax is definitely a good idea. I am rushing to finish using regular javascript. I will try your way later. Thanks
0

I ended up with a function to get the entire url -

function FullURL(){

var app = "<%=HttpRuntime.AppDomainAppVirtualPath.ToString()%>";

if (app == "/")
    app = "";

return location.protocol+'//'+location.hostname+(location.port ? ':'+location.port: '') + app;

}

And then call it from -

 function MyFun(action, param){          
       Windows.Location =FullURL() + "/MyController/" + action + "?param1=" + param;
    }

Comments

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.