Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6748823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:36:47+00:00 2026-05-26T12:36:47+00:00

If I create an controller method that returns ActionResult, then call View() at the

  • 0

If I create an controller method that returns ActionResult, then call View() at the end, is there any way to have a Filter (or something simular) change the returned result from the ViewResult emitted by View() to a PartialViewResult based on conditions in the Request?

Clearly I can have code in the controller method itself that returns the ViewResult or PartialViewResult, but this comes up in a number of places so it seems like a good use of a Filter. I tried doing this in both methods of the IResultFilter and saw no change in the output.

If this is not possible then I will look at creating some methods in my base controller called something like ViewOrPartial that I can call instead of View, but I wanted to seek broader wisdom before giving up on the Filter (or something like it) approach.

Thanks,
Matthew

Update: So it appears that at least one difference between what I had tried and what is now working based on Darin’s code is that I had overridden OnResultExecuting (and I had also tried OnResultExecuted) and Darin’s code overrides OnActionExecuted. Does that seem odd or am I perhaps overlooking something else? I am delighted to have it working but it seemed to make more sense that to attach it to the IResultFilter interface. I will have to look into the intention of the two interfaces more.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T12:36:48+00:00Added an answer on May 26, 2026 at 12:36 pm

    Yes, it is possible:

    public class MyActionFilterAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            var result = filterContext.Result;
            var viewResult = result as ViewResult;
            var someValue = filterContext.Controller.ValueProvider.GetValue("someValue");
            if (viewResult != null && someValue != null && someValue.AttemptedValue == "foo")
            {
                // if the controller action returned a view result
                // and the someValue parameter equals foo we replace the 
                // view result initially returned by the action by a 
                // partial view result
                var partialResult = new PartialViewResult();
                partialResult.ViewData.Model = viewResult.Model;
                filterContext.Result = partialResult;
            }
        }
    }
    

    Then decorate:

    [MyActionFilter]
    public ActionResult Foo()
    {
        MyViewModel vm = ...
        return View(vm);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a controller ActionResult that returns JSON that I would like to cache.
I have a controller method: public ActionResult Details(int id) { Order order = OrderFacade.Instance.Load(id);
I have a single view that is used by two Controller Actions. My Controller:
I have a controller that returns a list of files in a directory using
I know I can create a generic method that I can call from other
I have created a new method in one of the project's controllers that it
I am trying to redirect to the create method of another controller. However, I
I have a model called Details, and two controller methods new and create. New
What's the best way to dynamically create a controller in Rails. I've got a
In my app, I create view controller objects as I need them. When a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.