1

I have many controllers which are dependent on one variable which is let's say projectId.

So on every request of all methods I have to parse projectId as below. so is it possible to get this value in base controller variable and use it directly for all methods. I found some actionexecuting and initialize way, but they were examples in .net, I could not manage to get in asp.net core web API.

public void Save([FromBody]JObject data)
        {
            new SaveMethod((int)data["projectId"])
                .Save((string)data["otherData"]);
        }
2
  • 1
    Any reason you aren't using model binding to get a strongly-typed input parameter? Commented Sep 17, 2019 at 23:47
  • 1
    i have to create lot of models each method. :( Commented Sep 18, 2019 at 15:15

1 Answer 1

2

You can simply use the default model binding , create a class and application will automatically bind to object after getting the post request :

[HttpPost]
public void Post([FromBody] MyValue value)
{
}

public class MyValue {
    public int projectID { get; set; }

    //Other property 
} 

And the request will be something like :

POST https://localhost:XXXX/api/values
Content-Type: application/json

{"projectID": 1}

Then you can save to session for later access if you want the data available across requests.

Or you can just not use model binding(remove [FromBody] parameter , model binding execute before filter) , and get the request body in OnActionExecuting and save to session for later access :

public class MyControllerBase: Controller
{
    public override void OnActionExecuting(ActionExecutingContext context)
    {
        base.OnActionExecuting(context);


        var bodyStr = "";
        var req = context.HttpContext.Request;

        // Allows using several time the stream in ASP.Net Core
        req.EnableRewind();

        // Arguments: Stream, Encoding, detect encoding, buffer size 
        // AND, the most important: keep stream opened
        using (StreamReader reader
                = new StreamReader(req.Body, Encoding.UTF8, true, 1024, true))
        {
            bodyStr = reader.ReadToEnd();
        }

        // Rewind, so the core is not lost when it looks the body for the request
        req.Body.Position = 0;

    }

}

And you can make your controller inherits MyControllerBase and modify that to meet your requirement .

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

3 Comments

I am using .net core web api. in this onactionexecuting is not working. it gives error as "no suitable method found to override".
using model binding, I will have to create lot of models. per method.
ok got it. I was inheriting controllerbase instead of controller. thats why onactionexecuting was not working.

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.