这个错误的原因是什么? (.Net 5,C#)

What is the reason for this error? (.Net 5, C#)

我正在编写一个动态网站,一切正常,但我发现终端出现错误。

错误:

fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
  An unhandled exception has occurred while executing the request.
  Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Cannot perform runtime binding on a null reference
     at CallSite.Target(Closure , CallSite , Object )
     at AspNetCore.Views_Content_CategoryContent.ExecuteAsync() in C:\Users\lenovo\Desktop\Projeler\Wutemp\Views\Content\CategoryContent.cshtml:line 2
     at Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageCoreAsync(IRazorPage page, ViewContext context)
     at Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageAsync(IRazorPage page, ViewContext context, Boolean invokeViewStarts)
     at Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderAsync(ViewContext context)
     at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ViewContext viewContext, String contentType, Nullable`1 statusCode)
     at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ViewContext viewContext, String contentType, Nullable`1 statusCode)
     at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ActionContext actionContext, IView view, ViewDataDictionary viewData, ITempDataDictionary tempData, String contentType, Nullable`1 statusCode)       
     at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor.ExecuteAsync(ActionContext context, ViewResult result)
     at Microsoft.AspNetCore.Mvc.ViewResult.ExecuteResultAsync(ActionContext context)
     at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResultFilterAsync>g__Awaited|29_0[TFilter,TFilterAsync](ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
     at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context)
     at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted)
     at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters()
  --- End of stack trace from previous location ---
     at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
     at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
     at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
     at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
  --- End of stack trace from previous location ---
     at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
     at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
     at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
     at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
     at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
     at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
     at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

Controller(编辑:我不小心删除了ViewBag部分):

    [Route("Content/{url}/{id}")]
    public IActionResult Index(int id)
    {
        var content = repoContents.TGet(id);

        //If content has a category, go to CategoryContent
        if(content.ContentCategory != null && content.ContentCategory != 0){
            return RedirectToAction("CategoryContent", new 
            { 
                category = repoContentCategories.TGet((int)content.ContentCategory).Title, 
                url = content.URL, 
                id = content.ID 
            });
        }
        else{
            ViewBag.Content = content;
            return View();
        }
    }

问题是什么,我该如何解决?

我解决了问题。

我使用模型而不是评论中提到的 ViewBag 它不起作用我一直收到同样的错误

我认为问题是由于2个视图的路线部分相似。

索引:

[Route("Content/{url}/{id}")]
public IActionResult Index(int id)
{
    var content = repoContents.TGet(id);

    //If content has a category, go to CategoryContent
    if(content.ContentCategory != null && content.ContentCategory != 0){
        return RedirectToAction("CategoryContent", new 
        { 
            category = repoContentCategories.TGet((int)content.ContentCategory).Title, 
            url = content.URL,
            id = content.ID
        });
    }
    else{
        ViewBag.Content = content;
        return View();
    }
}

类别内容:

[Route("Content/{category}/{url}/{id}")]
public IActionResult CategoryContent(int id)
{
    ViewBag.Content = repoContents.TGet(id);
    return View();
}

我通过更改 ContentCategory 页面的 Route 部分解决了这个问题

[Route("Content/{category}/{url}/{id}")]

           |
           V
          
[Route("Category-Content/{category}/{url}/{id}")]