简体   繁体   中英

asp.NET Razor new Page error

Im pretty new to Razor and asp.NET so please dont judge me if my question might seem somehow stupid.

I created a razor project in VS2013 and now I created a new cshtml page. I only added some small things like labels and input fields there. I then wanted to link the template Index page to my own page with a simple:

<a href="~/Views/Acquisition/AC_Create.cshtml" class="btn btn-primary btn-lg">Login &raquo;</a>

When I then try to click on the link in the browser I get the error that the resource could not be found.

If I open the page in the "Page Inspector" the error display is the same with the additional information:

    <!--[HttpException]: A public action method &#39;User&#39; was not found on controller &#39;VitaminB.Controllers.UserController&#39;.
   bei System.Web.Mvc.Controller.HandleUnknownAction(String actionName)
   bei System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState)
   bei System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
   bei System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
   bei System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult)
   bei System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller)
   bei System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
   bei System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
   bei System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult)
   bei System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult)
   bei System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState)
   bei System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
   bei System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
   bei System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)
   bei System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result)
   bei System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   bei System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)-->

Sorry for the bad formating, trying my best =/

That's not how you create a link in MVC: Try using @Html.ActionLink(...)

(you should read some "getting started with MVC" type tutorials: you need to learn about routing, you never link to a .cshtml file directly)

in fact, when you type a URL in the url bar (same as your link) , you have to point the controller , not the view . Then, once reached, Controller can call a view . So you always have to point a controller that maybe just shiw you a view . View can only be pointed from a controller .

Link:

<a href="/somecontroller/someaction/123">link text</a>

or better:

@Html.ActionLink("link text", "someaction", "somecontroller", new { id = "123" }, null)

In the somecontroller controller:

public ActionResult someaction(int id)
{
 // process
 return View('viewname'); // viewname.cshtml must be present in /View/somecontroller/
}

NB: if the view is the same name ad the controller (somecontroller is the name of the controller and of the view ), you can just

return View();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM