Phish Phish - 1 year ago 81
C# Question

C# Web Services Routing Controller to Defined URL

I am new to C#, committing to a little Spike. I have built a few POCO OWIN self-hosting services and am trying to create a controller but guides and documentation everywhere appear to be pointing me in the incorrect direction.

I am adding default mapping to a configuration method inside a startup class. Then in my controller I have a simple GET method. This works like a charm, when I send a request to it using the url defined in my startup routing the method gets invoked. But now I need to set up a second method to be invoked by a new url.

I don't understand what I am missing and what I am not understanding but everything I attempt does not work for me:

relevant code in the controller:

// GET api/ManagedService
public string[] Get()
return new string[]
"Job Processed."

Relevant code in my startup class:

HttpConfiguration config = new HttpConfiguration();
name: "DefaultApi",
routeTemplate: "api/{controller}/start"

So on visiting api/{controller}/start I invoke my get but I want to invoke a different method with api/{controller}/receiptandsend.

Answer Source

I'm not 100% sure I understand what the problem is but try to change your routeTemplate to "api/{controller}/{action}" instead. The routeTemplate you are using assumes that you have an action (method) called start on every controller. Alternatively you could try to add another route, something like name: "anotherOne", routeTemplate: "api/{controller}/receiptandsend" but I don't think that is really the way to do it correctly.

edit: By the way, your Get method should probably be named something else, like StartJob or something, "Get" seems a bit misleading seeing that you don't get anything back except a text saying that you started some other process.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download