1

Similar to SO ASP.NET MVC: Many routes -> always only one controller:

O have a .net 4.7 MVC project project

my config route are as follows (following from the above post)

config.Routes.MapHttpRoute(
    name: "AllRoutes",
    routeTemplate: "{*url}",
    defaults: new
    {
      controller = "base",
    });

my base controller in my .net 4.7 project

public class BaseController : ApiController
{
  [HttpGet]
  public IHttpActionResult Get(HttpRequestMessage request)
  {
    return Ok();
  }

  [HttpPost]
  public IHttpActionResult Post(HttpRequestMessage request)
  {
    return Ok();
  }

  [HttpPut]
  public IHttpActionResult Put(HttpRequestMessage request)
  {
    return Ok();
  }

  [HttpDelete]
  public IHttpActionResult Delete(HttpRequestMessage request)
  {
    return Ok();
  }
}

now I'm porting my project into a .NET Core 2.0

I can't seem to setup the same thing

my config in the .net core project is as follows

 app.UseMvc(routes =>
  {
    routes.MapRoute(
      name: "AllRoutes",
      template: "{*url}",
      defaults: new
      {
        controller = "Base"
      }
    );

my base controller for my .net core project

//[Route("api/[controller]")]
public class BaseController : Controller
{
  [HttpGet]
  public IActionResult Get()
  {
    return Ok("get success");
  }

  // POST api/values
  [HttpPost]
  public IActionResult Post([FromBody]string value)
  {
    return Ok("post success");
  }

  [HttpPut]
  public IActionResult Put([FromBody]string value)
  {
    return Ok("put success");
  }

  [HttpDelete]
  public IActionResult Delete()
  {
    return Ok("delete success");
  }
}

any ideas?

2
  • You should be able to slap a [Route("/")] onto the controller to make this work without having you to configure any MVC routing rules up front. Commented Sep 6, 2017 at 7:36
  • i'm needed to setup the routes dynamically as it requires to read some config file, so certain url goes to 1 controller and others to another Commented Sep 7, 2017 at 21:43

3 Answers 3

3

Why do you even want to use MVC, when you have no controllers or routes?

Just use a custom middleware:

// Startup configure
app.Use(async (context, next) => 
{
    var service = context.RequestServices.GetRequiredServce<MyService>();
    var service.Execute();

    async next();
});

Update

Just in case it's not clear, you can inject IHttpContextAccessor in your service, where you can directly access the request stream and do whatever you need to do with it.

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

2 Comments

how do you use the above code i'm not following? basically i needed to setup the routes dynamically as it requires to read some config file, so certain url goes to 1 controller and others to another –
Your example above doesn't have any routes at all, just a general slug route. That's same as if you don't use routing middleware at all. You can remove all the controllers (which is just a single one in your case) and directly use HttpContext and the request within it to read the data, including the requested path HttpContext.Request.Path iirc. There is no need to have the overhead of the routing middleware, when you are not using routes at all
-1
public class BaseContoller : Controller {

[HttpGet("/base/get")]
public IActionResult Get() {
  return Ok("get success");
}
[HttpPost("/base/post")]
public IActionResult Post() {
return Ok("post success");
}
}

you looking for something like this?

1 Comment

this doesn't answer the question, i'm after eg... http ://someurl/api/person -> goes to basecontroller http ://someurl/api/organisation -> goes to basecontroller etc....
-1

or if you want to route this links you need add something like this

public class BaseController : Controller {
[Route("/get")]
public IActionResult Get() {
return Ok("get success");
}
}

Comments

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.