如何忽略MVC中没有实现Icontroller错误?

时间:2022-02-07 04:03:56

I have a controller class as below:

我有一个控制器类如下:

public partial class GenericViewController : Controller
    {
        public virtual void StoreUrl(string url)
        {
            Session["URL"] = url;
        }
    }

I am trying to place a call to this method using jquery ajax as below:

我试图使用jquery ajax调用此方法,如下所示:

    $.ajax(
                {
                    type: "POST",
                    cache:false,
                    url: "http://" + baseUrl + "/GenericView/StoreUrl",
                    data: { url: applicationurl },
                    error: Error
                });

Now when I trigger the ajax call I get the following error

现在,当我触发ajax调用时,我收到以下错误

[HttpException]: The controller for path '/GenericView/StoreUrl' was not found or does not implement IController.
   at System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType)
   at System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName)
   at System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory)
   at System.Web.Mvc.MvcHandler.<>c__DisplayClass6.<BeginProcessRequest>b__2()
   at System.Web.Mvc.SecurityUtil.<>c__DisplayClassb`1.<ProcessInApplicationTrust>b__a()
   at System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f)
   at System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action)
   at System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust[TResult](Func`1 func)
   at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state)
   at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state)
   at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData)
   at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

I have tried doing the following to resolve this in the register routes, but I still receive the same errors

我已尝试执行以下操作以在寄存器路由中解决此问题,但我仍然收到相同的错误

 routes.IgnoreRoute("Ignore/");
 routes.IgnoreRoute("{*GenericView}", new { GenView= @"(.*/)?GenericView(/.*)?" });
 routes.RouteExistingFiles = false;
 routes.IgnoreRoute("GenericView/{*pathInfo}");

An Additional Note: This code is hosted in an apllication called LookupData which hosted as application under an IIS website which mapped with url abc.com, then abc.com hosts a application which does authentication and abc.com/LookupData will hit the application which host the above code. Lookupdata when run alone does not throw this error, however when coming via abc.com the application is unable to resolver the GenericViewController.

附加说明:此代码托管在名为LookupData的应用程序中,该应用程序作为应用程序托管在IIS网站下,与url abc.com映射,然后abc.com托管一个进行身份验证的应用程序,abc.com/LookupData将命中应用程序托管上面的代码。 Lookupdata单独运行时不会抛出此错误,但是当通过abc.com发送时,应用程序无法解析GenericViewController。

I dont understand where is the mistake in my code. Can anyone please suggest a solution?

我不明白我的代码中的错误在哪里。有人可以建议一个解决方案吗?

1 个解决方案

#1


1  

One of your solutions to resolve this issue actually added a bug:

解决此问题的解决方案之一实际上添加了一个错误:

routes.IgnoreRoute("GenericView/{*pathInfo}");

This causes Mvc to ignore any route to the "GenericViewController" - except, maybe, when you're not using "convention over configuration" - thus not calling any method (like StoreUrl) on it.

这导致Mvc忽略到“GenericViewController”的任何路由 - 除非,当你不使用“约定优于配置”时 - 因此不会在其上调用任何方法(如StoreUrl)。

Adding

添加

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

might have the same result.

可能会有相同的结果。

Just remove the routes.IgnoreRoute("{*GenericView}"

只需删除routes.IgnoreRoute(“{* GenericView}”

#1


1  

One of your solutions to resolve this issue actually added a bug:

解决此问题的解决方案之一实际上添加了一个错误:

routes.IgnoreRoute("GenericView/{*pathInfo}");

This causes Mvc to ignore any route to the "GenericViewController" - except, maybe, when you're not using "convention over configuration" - thus not calling any method (like StoreUrl) on it.

这导致Mvc忽略到“GenericViewController”的任何路由 - 除非,当你不使用“约定优于配置”时 - 因此不会在其上调用任何方法(如StoreUrl)。

Adding

添加

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

might have the same result.

可能会有相同的结果。

Just remove the routes.IgnoreRoute("{*GenericView}"

只需删除routes.IgnoreRoute(“{* GenericView}”