MVC - problem with binding to another controller / action

I have a view in what I will call ControllerA from which I want a href / link for a view in another controller (ControllerB) and this view (ViewB) will appear in another browser tab. I tried the obvious method.

From ViewA ..

<a href="/ControllerB/ActionB" target="_blank">SomeText</a> 

      

ActionB performs the normal View () return;

the problem is when I click on the link a new tab opens, but I get this error.

Resource is not found. Description: HTTP 404. The resource you are looking for (or its dependencies) may have been deleted, its name changed, or is temporarily unavailable. Review the following URL and make sure it is spelled correctly.

Requested URL: / ControllerB / ActionB

I do not understand. I can guarantee 100% that this controller exists in the Controllers / ControllerB directory in the project.

and yes i tried it too ...

<a href="@Url.Action("ActionB", "ControllerB")" target="_blank">SomeText</a>

      

but the same problem.

which did (sort of) work, although there was an href setting in the ViewA to call the jscript function (href = "javascript: function ()"). this function uses jquery ajax call to navigate to ControllerB (using the same url as above) and this actually works (it stops on View () returning in the debugger). the problem is that nothing else happens. the new tab doesn't open and the view doesn't appear anywhere.

how can i do this / what am i doing wrong?

edit: Tried this too with the same result.

@Html.ActionLink("SignalR", "SRStart", "SignalR", null, new { target = "_blank" }) 

      

something to do with routing maybe?

edit # 2: I've seen this version of the bug before. depending on how I configured the url, I'll get a more verbose 404 error, the details of which include this.

The requested URL is http: // localhost: 64528 / Home / ~ / SignalR / SRStart

note that the main controller (or directory - not sure) is added by default to the front of the url, which I point out what is causing the error. not sure why this is happening, other than the fact that the view containing the reference is disconnected from the Home controller (ControllerA), but it does happen and it's a pain in the butt. so my guess is now the question is - how can I prevent the current controller (Home) from being added to the front of the url? somewhere in some application?

0


source to share


1 answer


Ok, that's interesting. when I submitted my action method, I just changed the attribute from [HttpPost] to [HttpGet] without thinking it would make any difference due to a startup problem, but then I ran it again and it worked. I'm not going to pretend to understand why Home was added to the url when I had Post as an attribute, but I guess it doesn't matter. so the attribute was bound to the action method. interesting. thanks everyone ..



+1


source







All Articles