As we all know, the MVC framework for ASP.NET uses the .NET routing engine, introduced in .NET 3.5, for generating and resolving URLs at runtime. As such, we can use the UrlHelper
class to query the routing engine and generate the correct URL for us. For example:
<a href='<%= Url.Action("MyAction", "MyController") %>'>Foo<a>
will generate the following at runtime:
<a href='/MyController/MyAction'>Foo<a>
This is great, as it means any changes we make to our routing rules are automagically reflected any affected URLs. Additionally, the routing engine will take into account where on our web-server our application is located. In the example above, if our application were deployed to virtual directory called MySite
, the routing engine would generate the following:
<a href='/MySite/MyController/MyAction'>Foo<a>
Well that's fine and dandy, but what happens when we want to use URLs in JavaScript? Consider the following simple scenario: A simple web-page with a text-box and a button. You enter a person ID into the text-box, click the button; and via an AJAX request, the person's details are displayed on the page. We have a controller:
// C#
public class PersonController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult Details(int id)
{
PersonService service = new PersonService();
Person person = service.GetPerson(id);
return PartialView(person);
}
}
' Visual Basic
Public Class PersonController
Inherits System.Web.Mvc.Controller
Public Function Index() As ActionResult
Return View()
End Function
Public Function Details(ByVal id As Integer) As ActionResult
Dim service As PersonService = New PersonService()
Dim person As Person = service.GetPerson(id)
Return PartialView(person)
End Function
End Class
A view:
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>
<!DOCTYPE html>
<html>
<head runat="server">
<title>Person</title>
<script type="text/javascript" src='<%= Url.Content("~/Scripts/jquery-1.5.1.min.js") %>'></script>
</head>
<body>
<div>
<input id="personId" type="text" />
<input id="getPersonButton" type="button" value="Get Person" />
</div>
<div id="personPlaceHolder">
</div>
<script type="text/javascript">
$(document).ready(function () {
$("#getPersonButton").click(function () {
var id = $("#personId").val();
getPerson(id);
});
});
function getPerson(id) {
var url = '/Person/Details/' + id;
$.get(url, function (result) {
$("#personPlaceHolder").html(result);
});
}
</script>
</body>
</html>
<%@ Page Language="VB" Inherits="System.Web.Mvc.ViewPage" %>
<!DOCTYPE html>
<html>
<head runat="server">
<title>Person</title>
<script type="text/javascript" src='<%= Url.Content("~/Scripts/jquery-1.5.1.min.js") %>'></script>
</head>
<body>
<div>
<input id="personId" type="text" />
<input id="getPersonButton" type="button" value="Get Person" />
</div>
<div id="personPlaceHolder">
</div>
<script type="text/javascript">
$(document).ready(function () {
$("#getPersonButton").click(function () {
var id = $("#personId").val();
getPerson(id);
});
});
function getPerson(id) {
var url = '/Person/Details/' + id;
$.get(url, function (result) {
$("#personPlaceHolder").html(result);
});
}
</script>
</body>
</html>
And a partial view:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Person>" %>
<fieldset>
<legend>Person</legend>
<table>
<tbody>
<tr>
<th>
ID
</th>
<td>
<%: Html.DisplayFor(model => model.Id) %>
</td>
</tr>
<tr>
<th>
Name
</th>
<td>
<%: Html.DisplayFor(model => model.Name) %>
</td>
</tr>
<tr>
<th>
Date of Birth
</th>
<td>
<%: Html.DisplayFor(model => model.DateOfBirth) %>
</td>
</tr>
</tbody>
</table>
</fieldset>
<%@ Control Language="VB" Inherits="System.Web.Mvc.ViewUserControl(Of Person)" %>
<fieldset>
<legend>Person</legend>
<table>
<tbody>
<tr>
<th>
ID
</th>
<td>
<%: Html.DisplayFor(Function(model) model.Id)%>
</td>
</tr>
<tr>
<th>
Name
</th>
<td>
<%: Html.DisplayFor(Function(model) model.Name)%>
</td>
</tr>
<tr>
<th>
Date of Birth
</th>
<td>
<%: Html.DisplayFor(Function(model) model.DateOfBirth)%>
</td>
</tr>
</tbody>
</table>
</fieldset>
Now look closely at the getPerson()
function:
function getPerson(id) {
var url = '/Person/Details/' + id;
$.get(url, function (result) {
$("#personPlaceHolder").html(result);
});
}
You will see that the URL is hard-coded and the person ID is simply concatenated onto the end. Clearly this will break if we either change our routing rules, or deploy the application to anywhere other the root of our web-server. One solution is to place an ASP.NET call to the routing engine within the string literal used for the URL:
// C#
function getPerson(id) {
var url = '<%= Url.Action("Details", "Person", new { Id = -999 }) %>'.replace('-999', id);
$.get(url, function (result) {
$("#personPlaceHolder").html(result);
});
}
// Visual Basic
function getPerson(id) {
var url = '<%= Url.Action("Details", "Person", New With {.Id = -999}) %>'.replace('-999', id);
$.get(url, function (result) {
$("#personPlaceHolder").html(result);
});
}
Note how we use a 'dummy' value of "-999" for the person ID, to ensure that the routing engine resolves the URL correctly. We then replace this value with the 'real' ID at runtime.
However, what if we want to move the getPerson()
function to a separate .js file? Our ASP.NET call to the routing engine will no longer work as the .js file is not processed server-side. The solution is to use a JavaScript helper object:
// C#
function UrlHelper() {
this.personDetails = function (id) {
return '<%= Url.Action("Details", "Person", new { Id = -999 }) %>'.replace('-999', id);
}
}
// Visual Basic
function UrlHelper() {
this.personDetails = function (id) {
return '<%= Url.Action("Details", "Person", New With {.Id = -999}) %>'.replace('-999', id);
}
}
The helper object must still remain in the view, as the call to the routing engine needs to be processed server-side. However, our getPerson()
function can now be modified to use the helper object and be safely moved into a separate .js file:
function getPerson(id) {
var urlHelper = new UrlHelper();
var url = urlHelper.personDetails(id);
$.get(url, function (result) {
$("#personPlaceHolder").html(result);
});
}
Create your free blog with Blogger. Your blog is whatever you want it to be.http://stbck.blogspot.com/
ReplyDeleteOf course, women seeking men you can avoid scammers if you are going too joined online connect websites that will requires you a payhttp://blog.udn.com/aa3510130/article
ReplyDeleteHome Page Blog About URL Analyse Contact Register/Login Top Meta Tag Whois Pagehttp://shangyun.mmidv.com/
ReplyDelete台中一夜情你喜歡的我們都有喔.一夜情這裡有最優質的指油壓台北台中高雄外送茶,我知道你需要點進來指油壓看看我會幫你選擇最符合您的條件的妹外送茶喔最勁爆的指油壓台北全套和援交妹-一夜情.
ReplyDelete援交日記Well that's fine and外送茶 dandy, but what happens when we want 外約to use URLs in JavaScript? Consider the高雄援交 following simple 炮友scenario: A simple web-page with a 援交text-box 台中茶訊and a button. You enter a person ID into 援交妹the text-box, click the button
ReplyDelete按摩個人工作室Well that's fine and dandy,兼職援 but what happens when we want to use URLs in JavaScript援交妹? Consider the following simple 台北援交scenario:援交妹 A simple web-page with a text-box and a button. You enter a person ID into the text-box, click the button台北援交
ReplyDelete