2014-09-22 2 views
9

Ich habe folgende Controller-Methode:Url.Link Würfen Nicht Ausnahme in Web API implementiert 2

[Authorize] 
    public IHttpActionResult Post(AlertDataModel model) 
    { 
     var userID = this.User.Identity.GetUserId(); 
     var alert = new Alert 
     { 
      Content = model.Content, 
      ExpirationDate = DateTime.Now.AddDays(5), 
      UserId = userID 
     }; 

     this.Data.Alerts.Add(alert); 
     this.Data.SaveChanges(); 

     var returnedAlert = new AlertDataModel 
     { 
      ID = alert.ID, 
      Content = alert.Content 
     }; 
     var link = Url.Link(routeName: "DefaultApi", routeValues: new { id = alert.ID }); 
     var uri = new Uri(link); 
     return Created(uri, returnedAlert); 
    } 

Aber ich habe NotImplementedException auf dieser Reihe:

var link = Url.Link (Route: "DefaultApi", routeValues: neu {id = alert.ID});

Hier ist der vollständige Fehler:

Message: "An error has occurred." 
ExceptionMessage: "The method or operation is not implemented." 
ExceptionType: "System.NotImplementedException" 
StackTrace: " at System.Web.HttpContextBase.get_Response()\ \ at System.Web.UI.Util.GetUrlWithApplicationPath(HttpContextBase context, String url)\ \ at System.Web.Routing.RouteCollection.NormalizeVirtualPath(RequestContext requestContext, String virtualPath)\ \ at System.Web.Routing.RouteCollection.GetVirtualPath(RequestContext requestContext, String name, RouteValueDictionary values)\ \ at System.Web.Http.WebHost.Routing.HostedHttpRouteCollection.GetVirtualPath(HttpRequestMessage request, String name, IDictionary`2 values)\ \ at System.Web.Http.Routing.UrlHelper.GetVirtualPath(HttpRequestMessage request, String routeName, IDictionary`2 routeValues)\ \ at System.Web.Http.Routing.UrlHelper.Route(String routeName, IDictionary`2 routeValues)\ \ at System.Web.Http.Routing.UrlHelper.Link(String routeName, IDictionary`2 routeValues)\ \ at System.Web.Http.Routing.UrlHelper.Link(String routeName, Object routeValues)\ \ at Exam.WebAPI.Controllers.AlertsController.Post(AlertDataModel model) in c:\\Users\\Kiril\\Desktop\\New folder\\Exam.WebAPI\\Controllers\\AlertsController.cs:line 63\ \ at lambda_method(Closure , Object , Object[])\ \ at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)\ \ at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)\ \ at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)\ \ --- End of stack trace from previous location where exception was thrown ---\ \ at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\ \ at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\ \ at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext()\ \ --- End of stack trace from previous location where exception was thrown ---\ \ at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\ \ at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\ \ at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()\ \ --- End of stack trace from previous location where exception was thrown ---\ \ at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\ \ at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\ \ at System.Web.Http.Filters.AuthorizationFilterAttribute.<ExecuteAuthorizationFilterAsyncCore>d__2.MoveNext()\ \ --- End of stack trace from previous location where exception was thrown ---\ \ at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\ \ at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\ \ at System.Web.Http.Controllers.AuthenticationFilterResult.<ExecuteAsync>d__0.MoveNext()\ \ --- End of stack trace from previous location where exception was thrown ---\ \ at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\ \ at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\ \ at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()" 

Ich habe folgendes Routing:

config.Routes.MapHttpRoute(
       name: "DefaultApi", 
       routeTemplate: "api/{controller}/{id}", 
       defaults: new { id = RouteParameter.Optional } 
      ); 

Ich habe versucht, den Code und die Fehler zu dekompilieren wurde in ReflectedHttpActionDescriptor.ExecuteAsync Methode geworfen.

Irgendwelche Ideen?

+0

Vielleicht dieser Frage bei der Suche nach einer Lösung unterstützen: http://stackoverflow.com/questions/15022627/url-link-not-working-in-webapi –

+0

Ich sah es, aber leider war es nicht sehr hilfreich für meinen Fall. – Elinos

+1

Ich bekomme jetzt den Fehler und habe stundenlang versucht, es zu lösen. Hast du eine Lösung gefunden? – Luke

Antwort

0

Der Name der Route ist falsch. Sie müssen das Attribut route der API-Methode mit einem bestimmten Namen versehen und dann auf diesen Namen verweisen. Beispiel:

[Route(Template = "{id}", Name = "GetThingById")] 
public IHttpActionResult Get(int id) { 
    return Ok(); 
} 

public IHttpActionResult DoStuff() { 
    return Ok(Url.Link("GetThingById", new { id = 5 }); 
} 
9

Wenn Sie OWIN verwenden, stellen Sie sicher, dass Sie ein neues HttpConfiguration Objekt in der Startkonfiguration Methode verwenden:

public class Startup 
{ 
    public static OAuthAuthorizationServerOptions OAuthOptions { get; private set; } 
    public static string PublicClientId { get; private set; } 

    public void Configuration(IAppBuilder app) 
    { 
     var config = new HttpConfiguration(); 

     ConfigureWebApi(config); 

     ConfigureAuth(app); 

     app.UseWebApi(config); 
    } 

    ... 

} 

Es dauerte mehrere Stunden, um herauszufinden, dass Sie shouldn ‚t einen Verweis auf GlobalConfiguration verwenden, wenn OWIN mit:

GlobalConfiguration.Configure(WebApiConfig.Register); 
+0

guten Mann selbst. –

+0

Was für eine Antwort! Ich war im Begriff, verrückt zu werden, dann sah ich :) – stt106

+0

Warum? OWIN war so ein Schmerz. Eine komische Sache nach der nächsten. – Langdon

Verwandte Themen