mohaidar
mohaidar

Reputation: 4286

how to extend the UrlHlper to attach a new method to it in Asp.net MVC?

I am trying to extend the UrlHelper in asp.net Mvc but I do not know what is the issue in my code?

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

namespace workflow.helpers
{
    public static class CustomUrls
    {

            public static string baseUrl(this UrlHelper helper)
            {
                return String.Format("/workflow");
            }
    }
}

I do not know why I can't see the new method in the Intel sense.

Edit, Web.Config File

<namespaces>
  <add namespace="System.Web.Mvc" />
  <add namespace="System.Web.Mvc.Ajax" />
  <add namespace="System.Web.Mvc.Html" />
  <add namespace="System.Web.Routing" />
  <add namespace="System.Web.Optimization" />
  <add namespace="workflow.helpers" />
</namespaces>

Upvotes: 0

Views: 32

Answers (1)

Sirwan Afifi
Sirwan Afifi

Reputation: 10824

First include this line in your view:

@using workflow.helpers

Then you can use it in the view:

@Url.BaseUrl()

Upvotes: 2

Related Questions