Pass argument to a Controller from Action Filter












1















Can I pass an argument from Action Filter to a controller and specifically through the parameters (in ASP.NET Core) ?



For example:



public class CategoryController : Controller
{
[HttpGet]
[ServiceFilter(typeof(ApiFilter))]
public async Task<IActionResult> Index(string dataComingFromActionFilter)
{
//use dataComingFromActionFilter
}
}


And



public class ApiFilter: IActionFilter 
{
public void OnActionExecuting(ActionExecutingContext context)
{
//maybe something similar to
context.sendToActionAsArgument['dataComingFromActionFilter'] = "data";

}
}









share|improve this question

























  • Look promising: docs.microsoft.com/en-us/dotnet/api/…

    – ZorgoZ
    Jan 2 at 20:06











  • Yes it is available for ASP.NET MVC. I am looking for an equivalent function in ASP CORE

    – AbdulKarim
    Jan 2 at 20:09













  • Right, sorry. But you have the controller instance as the contexts property, could you use it? Or the RouteData?

    – ZorgoZ
    Jan 2 at 20:14






  • 1





    Define an interface that exposes a dictionary. Impéement that interface on the controller. Cast the controller property to the interface and conditionalky set the dictionarry in the filter. You should be able to see the values in the action method.

    – ZorgoZ
    Jan 2 at 20:29






  • 1





    Each request has its own unique context.

    – No Refunds No Returns
    Jan 2 at 20:44
















1















Can I pass an argument from Action Filter to a controller and specifically through the parameters (in ASP.NET Core) ?



For example:



public class CategoryController : Controller
{
[HttpGet]
[ServiceFilter(typeof(ApiFilter))]
public async Task<IActionResult> Index(string dataComingFromActionFilter)
{
//use dataComingFromActionFilter
}
}


And



public class ApiFilter: IActionFilter 
{
public void OnActionExecuting(ActionExecutingContext context)
{
//maybe something similar to
context.sendToActionAsArgument['dataComingFromActionFilter'] = "data";

}
}









share|improve this question

























  • Look promising: docs.microsoft.com/en-us/dotnet/api/…

    – ZorgoZ
    Jan 2 at 20:06











  • Yes it is available for ASP.NET MVC. I am looking for an equivalent function in ASP CORE

    – AbdulKarim
    Jan 2 at 20:09













  • Right, sorry. But you have the controller instance as the contexts property, could you use it? Or the RouteData?

    – ZorgoZ
    Jan 2 at 20:14






  • 1





    Define an interface that exposes a dictionary. Impéement that interface on the controller. Cast the controller property to the interface and conditionalky set the dictionarry in the filter. You should be able to see the values in the action method.

    – ZorgoZ
    Jan 2 at 20:29






  • 1





    Each request has its own unique context.

    – No Refunds No Returns
    Jan 2 at 20:44














1












1








1


1






Can I pass an argument from Action Filter to a controller and specifically through the parameters (in ASP.NET Core) ?



For example:



public class CategoryController : Controller
{
[HttpGet]
[ServiceFilter(typeof(ApiFilter))]
public async Task<IActionResult> Index(string dataComingFromActionFilter)
{
//use dataComingFromActionFilter
}
}


And



public class ApiFilter: IActionFilter 
{
public void OnActionExecuting(ActionExecutingContext context)
{
//maybe something similar to
context.sendToActionAsArgument['dataComingFromActionFilter'] = "data";

}
}









share|improve this question
















Can I pass an argument from Action Filter to a controller and specifically through the parameters (in ASP.NET Core) ?



For example:



public class CategoryController : Controller
{
[HttpGet]
[ServiceFilter(typeof(ApiFilter))]
public async Task<IActionResult> Index(string dataComingFromActionFilter)
{
//use dataComingFromActionFilter
}
}


And



public class ApiFilter: IActionFilter 
{
public void OnActionExecuting(ActionExecutingContext context)
{
//maybe something similar to
context.sendToActionAsArgument['dataComingFromActionFilter'] = "data";

}
}






asp.net-core .net-core action-filter






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 12 at 20:18









ekad

12.2k123742




12.2k123742










asked Jan 2 at 20:03









AbdulKarimAbdulKarim

1517




1517













  • Look promising: docs.microsoft.com/en-us/dotnet/api/…

    – ZorgoZ
    Jan 2 at 20:06











  • Yes it is available for ASP.NET MVC. I am looking for an equivalent function in ASP CORE

    – AbdulKarim
    Jan 2 at 20:09













  • Right, sorry. But you have the controller instance as the contexts property, could you use it? Or the RouteData?

    – ZorgoZ
    Jan 2 at 20:14






  • 1





    Define an interface that exposes a dictionary. Impéement that interface on the controller. Cast the controller property to the interface and conditionalky set the dictionarry in the filter. You should be able to see the values in the action method.

    – ZorgoZ
    Jan 2 at 20:29






  • 1





    Each request has its own unique context.

    – No Refunds No Returns
    Jan 2 at 20:44



















  • Look promising: docs.microsoft.com/en-us/dotnet/api/…

    – ZorgoZ
    Jan 2 at 20:06











  • Yes it is available for ASP.NET MVC. I am looking for an equivalent function in ASP CORE

    – AbdulKarim
    Jan 2 at 20:09













  • Right, sorry. But you have the controller instance as the contexts property, could you use it? Or the RouteData?

    – ZorgoZ
    Jan 2 at 20:14






  • 1





    Define an interface that exposes a dictionary. Impéement that interface on the controller. Cast the controller property to the interface and conditionalky set the dictionarry in the filter. You should be able to see the values in the action method.

    – ZorgoZ
    Jan 2 at 20:29






  • 1





    Each request has its own unique context.

    – No Refunds No Returns
    Jan 2 at 20:44

















Look promising: docs.microsoft.com/en-us/dotnet/api/…

– ZorgoZ
Jan 2 at 20:06





Look promising: docs.microsoft.com/en-us/dotnet/api/…

– ZorgoZ
Jan 2 at 20:06













Yes it is available for ASP.NET MVC. I am looking for an equivalent function in ASP CORE

– AbdulKarim
Jan 2 at 20:09







Yes it is available for ASP.NET MVC. I am looking for an equivalent function in ASP CORE

– AbdulKarim
Jan 2 at 20:09















Right, sorry. But you have the controller instance as the contexts property, could you use it? Or the RouteData?

– ZorgoZ
Jan 2 at 20:14





Right, sorry. But you have the controller instance as the contexts property, could you use it? Or the RouteData?

– ZorgoZ
Jan 2 at 20:14




1




1





Define an interface that exposes a dictionary. Impéement that interface on the controller. Cast the controller property to the interface and conditionalky set the dictionarry in the filter. You should be able to see the values in the action method.

– ZorgoZ
Jan 2 at 20:29





Define an interface that exposes a dictionary. Impéement that interface on the controller. Cast the controller property to the interface and conditionalky set the dictionarry in the filter. You should be able to see the values in the action method.

– ZorgoZ
Jan 2 at 20:29




1




1





Each request has its own unique context.

– No Refunds No Returns
Jan 2 at 20:44





Each request has its own unique context.

– No Refunds No Returns
Jan 2 at 20:44












1 Answer
1






active

oldest

votes


















2














you can use



 context.ActionArguments["dataComingFromActionFilter"] = "data";


for updating existing action parameter. If you need to add new parameter, then



context.ActionArguments.Add("dataComingFromActionFilter", "data"); 


then you can reach it through Controller Action






share|improve this answer
























  • Thank you for helping. Setting the parameter worked perfectly. And I have a question about adding a new parameter. How can I use the added parameter in the controller?

    – AbdulKarim
    Jan 3 at 16:55











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54012466%2fpass-argument-to-a-controller-from-action-filter%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














you can use



 context.ActionArguments["dataComingFromActionFilter"] = "data";


for updating existing action parameter. If you need to add new parameter, then



context.ActionArguments.Add("dataComingFromActionFilter", "data"); 


then you can reach it through Controller Action






share|improve this answer
























  • Thank you for helping. Setting the parameter worked perfectly. And I have a question about adding a new parameter. How can I use the added parameter in the controller?

    – AbdulKarim
    Jan 3 at 16:55
















2














you can use



 context.ActionArguments["dataComingFromActionFilter"] = "data";


for updating existing action parameter. If you need to add new parameter, then



context.ActionArguments.Add("dataComingFromActionFilter", "data"); 


then you can reach it through Controller Action






share|improve this answer
























  • Thank you for helping. Setting the parameter worked perfectly. And I have a question about adding a new parameter. How can I use the added parameter in the controller?

    – AbdulKarim
    Jan 3 at 16:55














2












2








2







you can use



 context.ActionArguments["dataComingFromActionFilter"] = "data";


for updating existing action parameter. If you need to add new parameter, then



context.ActionArguments.Add("dataComingFromActionFilter", "data"); 


then you can reach it through Controller Action






share|improve this answer













you can use



 context.ActionArguments["dataComingFromActionFilter"] = "data";


for updating existing action parameter. If you need to add new parameter, then



context.ActionArguments.Add("dataComingFromActionFilter", "data"); 


then you can reach it through Controller Action







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 2 at 22:52









Derviş KayımbaşıoğluDerviş Kayımbaşıoğlu

15.5k22042




15.5k22042













  • Thank you for helping. Setting the parameter worked perfectly. And I have a question about adding a new parameter. How can I use the added parameter in the controller?

    – AbdulKarim
    Jan 3 at 16:55



















  • Thank you for helping. Setting the parameter worked perfectly. And I have a question about adding a new parameter. How can I use the added parameter in the controller?

    – AbdulKarim
    Jan 3 at 16:55

















Thank you for helping. Setting the parameter worked perfectly. And I have a question about adding a new parameter. How can I use the added parameter in the controller?

– AbdulKarim
Jan 3 at 16:55





Thank you for helping. Setting the parameter worked perfectly. And I have a question about adding a new parameter. How can I use the added parameter in the controller?

– AbdulKarim
Jan 3 at 16:55




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54012466%2fpass-argument-to-a-controller-from-action-filter%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas