6

We trying to rewrite out old project to form old mvc to the asp.net core mvc. we used to attach another mvc project as reference and called the controller without problem. but on the new asp.net core MVC it seems it need another work around

enter image description here

so the webapplication9 added webapplication1 as reference. and call the canary but it return 404. it worked on old mvc.

the controller just return the plain content

namespace WebApplication1.Controllers
{
    public class CanaryController : Controller
    {
        public IActionResult Index()
        {
            return Content("Canary");
        }
    }
}

is there some part need to add to make webapplication9 able to call the webapplication1 controller?

on old MVC 5 we can just called it on URL http://webapplication9/Canary and it works. on asp.net core it return 404 or not found

2
  • Please show us relevant code. What your controller does? how do you perform the invocation of methods? Commented Aug 17, 2018 at 7:32
  • hi @stukselbax. I just added the controller.. it just return string. on the old MVC 5 it works perfectly and recognize on other project Commented Aug 17, 2018 at 8:00

1 Answer 1

6

First, your other projects shouldn't be an Web application, but a normal PCL.

Second, after you reference it, you need to tell ASP.NET Core that it should look for controllers there. For that reason the Application Parts Api exists.

services.AddMvc()
    .AddApplicationPart(typeof(CanaryController).GetTypeInfo().Assembly);

P.S. while the docs state it should work out of the box, when doing integration tests/unit tests on the controllers the .AddApplicationPart is still required, at least was the case in ASP.NET Core 2.0

Sign up to request clarification or add additional context in comments.

2 Comments

similar question, the syntax has changed a little in .net core 3 stackoverflow.com/questions/47617994/…
I managed to use AddApplicationPart on a web application and it worked fine, any gochas I should watch out for?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.