Fedri Qrueger
Fedri Qrueger

Reputation: 641

Use another controller from different project on asp.net core

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

Upvotes: 4

Views: 7035

Answers (1)

Tseng
Tseng

Reputation: 64150

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

Upvotes: 4

Related Questions