1

I am trying to write a custom route prefix in asp.net WebApi. Following is the class I have written (was following a book).

public class ApiVersion1RoutePrefixAttribute : RoutePrefixAttribute
 {
    private const string RouteBase = "api/{apiVersion:apiVersionConstraint(v1)}";
    private const string PrefixRouteBase = RouteBase + "/";

    public ApiVersion1RoutePrefixAttribute(string routePrefix)
        : base(string.IsNullOrWhiteSpace(routePrefix) ? RouteBase : PrefixRouteBase + routePrefix) { }

 }

When I have build the solution , I got the following Error:

"Error 1 'WebApiBook.Web.Common.Routing.ApiVersion1RoutePrefixAttribute': cannot derive from sealed type 'System.Web.Http.RoutePrefixAttribute'"

Here is my packages.config file: enter image description here

1 Answer 1

1

You are using Microsoft.AspNet.WebApi.Core version 5.0.0 which uses System.Web.Http version 5.0.0 And in this version RoutePrefixAttribute is marked as sealed so you can not extend it:

namespace System.Web.Http
{
  /// <summary>
  /// Annotates a controller with a route prefix that applies to all actions within the controller.
  /// </summary>
  [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
  public sealed class RoutePrefixAttribute : Attribute
  {
    /// <summary>
    /// Gets the route prefix.
    /// </summary>
    public string Prefix { get; private set; }

    /// <summary>
    /// Initializes a new instance of the <see cref="T:System.Web.Http.RoutePrefixAttribute"/> class.
    /// </summary>
    /// <param name="prefix">The route prefix for the controller.</param>
    public RoutePrefixAttribute(string prefix)
    {
      if (prefix == null)
        throw Error.ArgumentNull("prefix");
      this.Prefix = prefix;
    }
  }
}

Either derive from System.Web.Mvc.RoutePrefixAttribute or update your Microsoft.AspNet.WebApi.Core to latest version. Here is the implementation for version 5.2.3. As you can see the class is not selaed there:

namespace System.Web.Http
{
  /// <summary>
  /// Annotates a controller with a route prefix that applies to all actions within the controller.
  /// </summary>
  [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
  public class RoutePrefixAttribute : Attribute, IRoutePrefix
  {
    /// <summary>
    /// Gets the route prefix.
    /// </summary>
    public virtual string Prefix { get; private set; }

    /// <summary>
    /// Initializes a new instance of the <see cref="T:System.Web.Http.RoutePrefixAttribute"/> class.
    /// </summary>
    protected RoutePrefixAttribute()
    {
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="T:System.Web.Http.RoutePrefixAttribute"/> class.
    /// </summary>
    /// <param name="prefix">The route prefix for the controller.</param>
    public RoutePrefixAttribute(string prefix)
    {
      if (prefix == null)
        throw Error.ArgumentNull("prefix");
      this.Prefix = prefix;
    }
  }
}
Sign up to request clarification or add additional context in comments.

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.