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

  • SEARCH
  • Home
  • 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 3991472
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T06:37:12+00:00 2026-05-20T06:37:12+00:00

I want to do this: public ActionResult Details(int id) { Object ent = new{

  • 0

I want to do this:

   public ActionResult Details(int id)
    {
        Object ent = new{ prop1 = 1, prop2 = 2};
        if (Request.AcceptTypes.Contains("application/json"))
         return Json(ent, JsonRequestBehavior.AllowGet);

        ViewData.Model = ent;
        return View();
    }

But wonders if there isn’t a better way (and build in) to detect an incoming jsonrequest, similar to IsAjaxRequest. I would want to use the same url, so preferably don’t want to deal with format extensions, like “.json”, “.html” etc.

Also I don’t want to have a different url for the jsonrequest and the normal web request that returns a view.

  • 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-20T06:37:13+00:00Added an answer on May 20, 2026 at 6:37 am

    Using ActionFilterAttribute for your BaseController. and inherit all other controllers from BaseController

    [IsJsonRequest]
    public abstract class BaseController : Controller
    {
       public bool IsJsonRequest { get; set; }
    }
    
    The ActionFilterAttribute
    public class IsJsonRequest: ActionFilterAttribute  
    {  
        public override void OnActionExecuting(ActionExecutingContext filterContext)  
        { 
            var myController = filterContext.Controller as MyController;
            if (myController != null)
            {
    
                if (filterContext.HttpContext.Request.AcceptTypes.Contains("application/json"))
                {
                    myController.IsJsonRequest = true;
                }
                else
                {
                    myController.IsJsonRequest = false;
                }
            }
        }
    }
    
    public class TestController : BaseController
    {
        public ActionResult Details(int id)
        {
              if (IsJsonRequest)
                   return Json Data
              else
                   return view
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I want to show an image I use something like this: public ActionResult
I want to create an extension method like this public static bool AllConsecutives(this IEnumerable<int>
I have a controller action which renders a partial view: public ActionResult Details(int id)
I have this controller: public ActionResult Index(int id) { var cust = (from c
I want a method like this: public async void DoSomething() { DoSomethingElse(); } But
I want to serialize this class: public class CarDisplay { public string Name {
I want to do something like this: public class ScadenzaService { ... public List<Scadenza>
As in the title, I want to test a method like this: public void
I have a situation where i want to return List<> from this function public
given this class definition: public class Frame { IFrameStream CapturedFrom; } I want implement

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.