ASP.NET Core MVC API Controller

I’m trying to add in functionality for adding and removing from a list of items in the UI. The typical way of doing this was to post the form to a controller, add/remove the item, then return the partial view to append to the DOM. When trying to set up an API or MVC controller, the route is not working. I even set up Swagger to try to troubleshoot it, but while Swagger properly finds the controller and route, the route doesn’t actually work.

I can only assume that there is something in Servicestack that is hijacking how routing works. This is a legacy application that we took over and removing Servicestack is not an option. Ultimately, all I want to be able to do is hit a standard controller. If there is a Servicestack way of doing add/remove then I am open to this as well. Thanks.

If this .NET Framework then ServiceStack needs to be hosted at a different path e.g. /api to avoid route conflicts. See docs for running ServiceStack side-by-side with ASP .NET MVC:

https://docs.servicestack.net/mvc-integration

This is .NET Core 2.1 (yes it needs to be upgraded).

You can avoid path conflicts by hosting ServiceStack at a different PathBase:

app.UseServiceStack(new AppHost {
    PathBase = "/api",
});

Otherwise ServiceStack and MVC should be able to be configured in the same pipeline, you can refer to the mvc project template for an example of a working configuration. Although .NET Core 2.1 is pretty old and out of support so you should look at upgrading first.

This isn’t a path conflict. Attempting to access the controller route results in the 404 page being returned. It seems to me more like the routing is being hijacked somehow by your framework. Using your PathBase code causes the whole system to fail “No webpage was found for the web address”. Is there another setting somewhere I have to update as well? I went through and set the request paths to include the /api route.

If this is .NET Core, all requests are handled by the same middleware request pipeline, so unless you don’t have a wildcard route to handle fallback requests, the requests should be handled by the next middleware in the pipeline.

When using PathBase, all ServiceStack requests should start from there, e.g. /api/metadata.

The startup pretty much follows that template you have. There is almost no difference.

app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");                
            });

Does the mvc template work?

$ x new mvc ProjectName
$ cd ProjectName/ProjectName
$ dotnet run

MVC Route:

https://localhost:5001/Home/About

ServiceStack Route:

https://localhost:5001/hello?name=world

No, nothing in the template works when running it and trying to access those links.

Just followed those instructions and it works without issue:

Not sure what could be different with your environment that it yields different behavior.

I see this:

No idea, are you using the template’s default settings, i.e. .NET 6?

I just tested my other applications and they work just fine. Am I missing some sort of dependency or something? Let me pull your code off github and use that instead.

It’s the first I’ve heard of a .NET Core application behaving differently so I’ve no idea what the issue could be.

I just tried pulling your code down on 3 different computers. All of them failed to work. 2 of them were Windows 11 and the other was Windows 10.

EDIT: Never mind. I found the issue. The launch profile is defaulted to IIS Express. Using MyApp works.

One thing that our app uses is the Smart MVC Razor pages feature. Can this interfere at all?

Yeah, you wont be able to use RazorFormat and MVC, i.e. you’ll need to remove RazorFormat to use MVC Routing and Controllers.

Ok, what all is involved in that? Do you have a page somewhere on this?

As in don’t register the RazorFormat plugin if you’re using it:

//Plugins.Add(new RazorFormat());

Removing that causes the website to actually load as a download. I’m fairly certain there is more to removing this than just removing the plugin.

What website, are you talking about the mvc template?