tags:

views:

75

answers:

3

Hi,

EDIT I may not have worded my problem very well. The actual problem is not in the routing as much as it is in trying to derive the actual url of the request.

The application can be deployed on many seperate machines. For example

http://localhost:2990/Client/List

http://avapd024/llne.dev/Client/List

http://machine123:81/llne.prod/Client/List

etc etc

The reason I can't use Action.Link or html helpers is that I need to build the link before I return it to the view as it populates as grid taking JSON (datatables.net) and NOT and the time the view is being rendered

thanks

I need to populate a grid with a list of items as links pointing to a different controller and action and id.

for example; when I load the grid the url is as follows: http://localhost:2990/Client which is fine. The grid contains a link like the following

JSID = "<a href='http://" + RequestUrl + "/trainingdelivery/Get/" + referral.ReferralId + "'>Training Delivery</a>",

the controller is 'trainingdelivery' and action is 'Get' and id = referralid which results in a url like http://localhost:2990/trainingdelivery/Get/12345). This is fine on my local machine but when we deploy it goes to a server with a different url like http://avapd024/llne.dev:81/ as you can see the 'llne.dev:81' is the IIS web and I need to get the url to wherever it is to recognise its location and go to the correct controller and action.

I tried using the request object and I could probably hack around in there to get it but I would like to know if there are any better solutions?

thanks

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.IgnoreRoute("{resource}.js/{*pathInfo}");

            routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" });

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = "" } // Parameter defaults
                );
        }

AJAX CALL IN JQUERY

oTable = $('#referralTable').dataTable(
        {

            "bFilter": true,
            "bJQueryUI": true,
            "sPaginationType": "full_numbers",
            "bAutoWidth": false,
            "sAjaxSource": "../Client/Fetch",
            "aoColumns": [
            //client details
                            {"bVisible": false, "sName": "ReferralId" },
                            { "sTitle": "CRN", "sName": "CRN" },
                            { "sTitle": "JSID", "sName": "JSID" },
                            { "sTitle": "Name", "sName": "Name" }
]
});

the sAjaxSource is the call that loads the grid with data and returns JSON. By the time it is returned it needs to be formatted correctly.

+2  A: 

Yes! Use the HtmlHelper.ActionLink() method.

JSID = <%= Html.ActionLink("Get", 
            new { controller = "trainingdelivery", id = referral.ReferralId}) %>

This assumes that you have set up a route in your Global.asax file like the following:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace MyApp.MVC
{
  public class MvcApplication : System.Web.HttpApplication
  {
    public static void RegisterRoutes(RouteCollection routes)
    {
      routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

      routes.MapRoute(
        "TrainingDelivery",
        "trainingdelivery/{action}/{id}",
        new { controller = "TrainingDelivery", action = "Get", id = 0 }
      );
    }

    protected void Application_Start(object sender, EventArgs e)
    {
      RegisterRoutes(RouteTable.Routes);
    }
  }
}

Read more about ASP.Net Routing

jessegavin
Can't really use the action link because the it is populating a grid (datatables.net) that needs it to be in JSon format and the link is not built in the view (.spark file). The entire href needs to be built before it reaches the view as the grid is populated using a jquery ajax call. I'll post that in the original message as well. Thanks all the same
kurasa
A: 

I think you should use Domain routing as defined in this link.

Nitin Midha
A: 

The solution was to use Request.ApplicationPath to construct the url.

kurasa