ASP。Net MVC:为json请求创建一个区域

时间:2022-11-04 04:14:46

Just wondering what people think about creating an area to hold/manage json based requests (note I am thinking mostly get data not post data). I know its not your typical use of an area (i.e. normally you would create a different area for blog vs forum) but I am getting to the point where my project isn't huge but I definitely have a a lot of json stuff which seems to be confusing the issue and is making things look "unclean".

只是想知道人们对创建一个区域来保存/管理基于json的请求有什么看法(注意,我认为大多数情况下获取数据而不是发布数据)。我知道它不是你的典型的使用面积(即通常会创建一个不同的博客和论坛区)但我得到,我的项目不是很大但我绝对很多json的东西似乎令人困惑的问题,使事情看起来“不洁”。

For instance, at the bottom of each controller is where I am putting json actions and so that they don't get mixed up with the other actions I prefix them with json - I shouldn't have to do this... Also I have specific view models for json which I have to prefix with json as well... etc, etc.

例如,在每个控制器的底部都放置了json动作,这样它们就不会与其他动作混淆,我用json对它们进行前缀——我不应该这样做……此外,我还为json提供了特定的视图模型,我还必须用json作为前缀……等等,等等。

It would seem much cleaner to have them off in their own area and be able to drop the json prefix all together and have that defined by the area... what do you think or is this a bad idea?

把它们放在自己的区域,并且能够将json前缀放在一起,并在区域中定义它,看起来会更干净。你觉得这是个坏主意吗?

Cheers Anthony

欢呼声安东尼

3 个解决方案

#1


1  

I think it's a good idea. Having an asynchronous area where all controllers implement only asynchronous actions will certainly clean up your code. The problem will come when your project does become so big you want to expand into regular areas, then you'll end up with some naming conventions that might end up a bit messy.

我认为这是个好主意。拥有一个所有控制器只实现异步操作的异步区域,肯定会清除您的代码。当您的项目变得非常大,您想要扩展到常规区域时,问题就会出现,然后您将以一些命名约定结束,这些约定可能会有点混乱。

#2


0  

You could also just create a separate controller for your json actions. I think this makes more sense than creating an area. Do you need json specific views, content, model etc or just some asynchronous actions?

您还可以为json操作创建一个单独的控制器。我认为这比创建一个区域更有意义。您需要json特定的视图、内容、模型等等还是一些异步操作?

#3


0  

I think that it's not nessesery to create separate area or even separate actions. If the actions return the same data and differ only in the type of the request - ajax or non-ajax you can just check what is the request and use the corresponding data format.

我认为,创建单独的领域甚至是单独的行动都不是必要的。如果操作返回相同的数据,并且仅在请求的类型(ajax或非ajax)上有所不同,您只需检查请求是什么,并使用相应的数据格式。

public ActionResult Index()
{
    MyViewModel model = DataAccess.GetMyViewModel(); // Data access code
    if (Request.IsAjaxRequest())
    {
        return Json(model);
    }
    else
    {
        return View(model);
    }
}

#1


1  

I think it's a good idea. Having an asynchronous area where all controllers implement only asynchronous actions will certainly clean up your code. The problem will come when your project does become so big you want to expand into regular areas, then you'll end up with some naming conventions that might end up a bit messy.

我认为这是个好主意。拥有一个所有控制器只实现异步操作的异步区域,肯定会清除您的代码。当您的项目变得非常大,您想要扩展到常规区域时,问题就会出现,然后您将以一些命名约定结束,这些约定可能会有点混乱。

#2


0  

You could also just create a separate controller for your json actions. I think this makes more sense than creating an area. Do you need json specific views, content, model etc or just some asynchronous actions?

您还可以为json操作创建一个单独的控制器。我认为这比创建一个区域更有意义。您需要json特定的视图、内容、模型等等还是一些异步操作?

#3


0  

I think that it's not nessesery to create separate area or even separate actions. If the actions return the same data and differ only in the type of the request - ajax or non-ajax you can just check what is the request and use the corresponding data format.

我认为,创建单独的领域甚至是单独的行动都不是必要的。如果操作返回相同的数据,并且仅在请求的类型(ajax或非ajax)上有所不同,您只需检查请求是什么,并使用相应的数据格式。

public ActionResult Index()
{
    MyViewModel model = DataAccess.GetMyViewModel(); // Data access code
    if (Request.IsAjaxRequest())
    {
        return Json(model);
    }
    else
    {
        return View(model);
    }
}