T4MVC ArgumentOutOfRangeException in View

2019-09-13 20:24发布

问题:

I just installed T4MVC on my project and I run into some problem.

In my controller i can call redirect to action without problem:

  return RedirectToAction(Actions.Index());

If i do call it from my view, i get an ArgumentOutOfRangeException.

@Html.ActionLink("Delete Dinner", MVC.Home.Index())

To make sure I did it correctly, i created a new MVC solution and that line works. I removed from my HomeController my 'baseController' inheritance and resintalled T4MVC to be sure it wouldn't interfere. I've no more idea where even start to look for this, debug doesn't help me as it seems to explose in the extention method:

[ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index]
   System.ThrowHelper.ThrowArgumentOutOfRangeException() +72
   System.Collections.ObjectModel.Collection`1.set_Item(Int32 index, T value) +10419142
   System.Web.Mvc.ControllerContext.get_RequestContext() +25
   System.Web.Mvc.Html.LinkExtensions.RouteLink(HtmlHelper htmlHelper, String linkText, String routeName, String protocol, String hostName, String fragment, RouteValueDictionary routeValues, IDictionary`2 htmlAttributes) +47
   System.Web.Mvc.T4Extensions.ActionLink(HtmlHelper htmlHelper, String linkText, ActionResult result, IDictionary`2 htmlAttributes, String protocol, String hostName, String fragment) +196
   System.Web.Mvc.T4Extensions.ActionLink(HtmlHelper htmlHelper, String linkText, ActionResult result) +72
   ASP._Page_Views_Home_Index_cshtml.Execute() in c:\Thva\Misc\DropBox\Work\MyProjects\Wims\Wims.Website\Views\Home\Index.cshtml:53

Any idea? Thanks in advance

Edit: I just tried this and it still doesn't work: Create a new controller:

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

namespace Wims.Website.Controllers
{
    public partial class MyTestController : Controller
    {
        //
        // GET: /MyTest/

        public virtual ActionResult Index()
        {
            return View();
        }

    }
}

Run AutoT4MVC, View :

@{
    ViewBag.Title = "Index";
}
@Html.ActionLink("aiaieiae", MVC.MyTest.Index())
<h2>Index</h2>

To make sure i have no depedency, and it still doesn't work if i call my page:

http://localhost:2303/MyTest/index

回答1:

I had the same issue. Then I found the following warnings in build log:

> Consider app.config remapping of assembly "System.Web.Mvc, Culture=neutral, PublicKeyToken=31bf3856ad364e35" from Version "3.0.0.0" [C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 3\Assemblies\System.Web.Mvc.dll] to Version "4.0.0.0" to solve conflict and get rid of warning.
...and same warnings for System.Web.WebPages, System.Web.Razor, System.Web.WebPages.Deployment, System.Web.WebPages.Razor
> C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(1605,5): warning MSB3247: Found conflicts between different versions of the same dependent assembly.

I did what the warning message suggested by adding the following lines to web.config:

<configuration>
    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
                <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" culture="neutral" />
                <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
            </dependentAssembly>
        </assemblyBinding>
        ...the same for the other assemblies
    </runtime>
</configuration>

And that was it - T4MVC works fine. Sometimes, NuGet adds these lines automatically, I don't know why it didn't so this time.

This solution also explains why creating a new web project and copying source codes might solve this issue.



回答2:

Based on the stack, it looks like it has trouble accessing the request context, which is not related to T4MVC. To isolate, can you remove the T4MVC line and simply try to write:

@ViewContext.RequestContext

in your view? You can also try @Html.ViewContext.RequestContext. I'm guessing you'll see the same exception. If so, I'm not sure what causes it, but at least it gets you a step closer.

Another thing to try: does the regular MVC ActionLink work, or is it only T4MVC ActionLink that fails?



回答3:

I've looked into this with David Ebbo (thanks a lot to him for his time), and could not find why it's not working.

After copy pasting T4MVC code in my project with my own name space and use those methods it was working..

So, i just recreated a new MVC Website and reimported everything.. not fun but i believe T4MVC worths the trouble :)



回答4:

Ok, so I know my answer is rather late but it may help others. In my case everything went well until all of the sudden I had this error:

[ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index]
   System.Collections.ObjectModel.Collection`1.set_Item(Int32 index, T value) +14406551
   System.Web.Mvc.Html.LinkExtensions.RouteLink(HtmlHelper htmlHelper, String linkText, String routeName, String protocol, String hostName, String fragment, RouteValueDictionary routeValues, IDictionary`2 htmlAttributes) +89
   System.Web.Mvc.T4Extensions.ActionLink(HtmlHelper htmlHelper, String linkText, ActionResult result, Object htmlAttributes, String protocol, String hostName, String fragment) +338
   System.Web.Mvc.T4Extensions.ActionLink(HtmlHelper htmlHelper, String linkText, ActionResult result, Object htmlAttributes) +107
   ASP._Page_Views_Shared__Layout_cshtml.Execute() in c:\work\[...]\Views\Shared\_Layout.cshtml:29
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +280
   System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +126
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +196
   System.Web.WebPages.WebPageBase.Write(HelperResult result) +89
   System.Web.WebPages.WebPageBase.RenderSurrounding(String partialViewName, Action`1 body) +233
   System.Web.WebPages.WebPageBase.PopContext() +291
   Castle.DynamicProxy.AbstractInvocation.Proceed() +116
   Glimpse.Core.Extensibility.ExecutionTimer.Time(Action action) +85
   Glimpse.Core.Extensibility.AlternateMethod.NewImplementation(IAlternateMethodContext context) +186
   Castle.DynamicProxy.AbstractInvocation.Proceed() +604
   System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +378
   Castle.Proxies.AsyncControllerActionInvokerProxy.InvokeActionResult_callback(ControllerContext controllerContext, ActionResult actionResult) +21
   Castle.DynamicProxy.AbstractInvocation.Proceed() +116
   Glimpse.Core.Extensibility.ExecutionTimer.Time(Action action) +85
   Glimpse.Core.Extensibility.AlternateMethod.NewImplementation(IAlternateMethodContext context) +186
   Castle.DynamicProxy.AbstractInvocation.Proceed() +604
   System.Web.Mvc.<>c__DisplayClass1a.<InvokeActionResultWithFilters>b__17() +33
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +853420
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +265
   System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) +837892
   System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +28
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +65
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +51
   System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +42
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +51
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +606
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +288

In my case the problem was that inside web.config I have added an xmlns attribute to the configuration element:

<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">

Originally it was just

<configuration>

By deleting the xmlns I got my web.config warnings back but my site is working again and I can continue my work.

[Update]

I wanted to add what I think is the real solution to this problem but I saw that gius bellow already wrote it. So yeah, the issue is a missing <bindingRedirect> in web config:

<dependentAssembly>
   <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" culture="neutral" />
   <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>