4

I am trying to get the url for a known route name inside of a razor template.

Here is my partial view

@model ScripterEngine.ViewModels.CampaignViewModel

<script type="text/javascript">

    function logTime(stage, status, async){

        var target = "@UrlHelper.RouteUrl("timetracker.clockin")";

        var postData =
        {
            'campaign_id': @Model.id,
            'agent_id': is_system_agentid.value,
            'log_id': is_attr_calldata.tracker_id,
            'stage_name': stage
        };

        if( status == 'out'){
            target = "@UrlHelper.RouteUrl("timetracker.clockout")";
        }

        if( async !== false){
            async = true;
        }

        $.ajax({
            type: 'POST',
            url: target,
            data: postData,
            async: async,
            dataType: "json",
            error: function( jqXHR, textStatus, errorThrown ){

                alert('clock ' + status + ' failed!' + jqXHR.status );
            },
            success: function(data){

                if(data && data.id && status != 'out'){
                    is_attr_calldata.tracker_id  = data.id;
                }

            }
        });
    }

</script>   

Here is my route mapping

        //Timetracker - ClockIn
        routes.MapRoute(
            "timetracker.clockin",
            "timetracker/clockin",
            new { controller = "TimeTracker", action = "ClockIn" }
        );

        //Timetracker - ClockOut
        routes.MapRoute(
            "timetracker.clockout",
            "timetracker/clockout",
            new { controller = "TimeTracker", action = "ClockOut" }
        );

However, I get a compilation error after I start the program and navigate to the route .

Compilation Error Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0120: An object reference is required for the non-static field, method, or property 'System.Web.Mvc.UrlHelper.RouteUrl(string)'

The error point at this line

var target = "@UrlHelper.RouteUrl("timetracker.clockin")";

How can I properly get the Url from a giving route name?

1
  • @NikolaiDante yes. that should not matter. the outside double quotes are plain and the inside will be executed at the server side. Commented Feb 4, 2016 at 22:17

1 Answer 1

3

The UrlHelper is exposed on a view as @Url - the code for WebViewPage has:

public UrlHelper Url { get; set; }

Try:

var target = '@Url.RouteUrl("timetracker.clockin")';
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. That did the trick. Since this return only the URI and does not include the domain do I need to add a base tag to my html?
Personally, I wouldn't, it makes deploying through over environments (dev, test, staging, production etc..) more tricky. Unless you have a compelling reason to?
I just want to make sure that I can put this URI any where in the site and it will take the user to the URL
you should be ok... I've never had to use base in an app, but YMMV. :-) It should work out the right relative url.

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.