自定义ASP.NET MVC路由到服务与QUOT;以.json" "&的.xml QUOT;样式的URL [英] Customizing ASP.NET MVC Routing to service ".json" ".xml" Style Urls

查看:141
本文介绍了自定义ASP.NET MVC路由到服务与QUOT;以.json" "&的.xml QUOT;样式的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个搜索API我的工作,需要在HTML块(使用样式客户已在其终端定义)返回的搜索结果。我也想以JSON返回结果,为今后的阿比的东西,我们最终会使用。目前,该路线是这样的:

  / API / 1 /搜索/ JSON参数1 =胡说&放大器;参数2 =等等&放大器;等等
/ API / 1 /搜索/ HTML参数1 =胡说&放大器;参数2 =等等&放大器;等等

有关参考,这里的模式是/ {}面积/ 1 / {控制器} / {行动}

我喜欢的一些阿比的我见过取决于'延伸',他们在URL中的返回结果的不同格式的外观,一拉:

  /api/1/search.json?param1=blah&param2=blah&etc

不过,我还没有想出如何配置Asp.Net的MVC路由来支持这种风格。在ApiAreaRegistration.cs通用布线是:

  context.MapRoute(
    Api_default
    原料药/ 1 / {控制器} / {行动} / {ID}
    新{行动=索引,ID = UrlParameter.Optional});

我曾尝试以下,高于一般的定义,它不工作:

  //搜索API
context.MapRoute(
    searchJson
    API / 1 / {控制器} {}行动,
    新{控制器=SearchController});

我将如何配置路由,使.format风格的网址?


解决方案

  context.MapRoute(
    Api_default
    {}面积/ 1 / {控制器} {}格式,
    新{行动=索引,ID = UrlParameter.Optional});

可能是你想要的东西。然后,你可以只返回基于传入的参数不同的结果。

在一个API区的背景下,SearchController是这样的:

 公共类SearchController:控制器
{
    公众的ActionResult指数(字符串格式,SearchModel搜索)
    {
        VAR的结果= searchFacade.SearchStuff(搜索);        如果(format.Equals(XML))
            返回XML(结果);使用XmlResult或任何//
        如果(format.Equals(HTML))
            返回查看(结果);
        返回JSON(结果,JsonRequestBehavior.AllowGet);
    }
}

I have a search Api I'm working on that needs to return search results in a block of Html (using styles the client has defined on their end). I would also like to return results in Json, for future Api stuff we'll eventually be using. Currently, the routes look like this:

/api/1/search/json?param1=blah&param2=blah&etc
/api/1/search/html?param1=blah&param2=blah&etc

For reference, the pattern here is /{area}/1/{controller}/{action}.

I like the look of some Api's I've seen that return results in different formats depending on the 'extension' they have in the url, a la:

/api/1/search.json?param1=blah&param2=blah&etc

However, I haven't figured out how to configure Asp.Net's Mvc routing to support this style. The general routing in ApiAreaRegistration.cs is:

context.MapRoute(
    "Api_default",
    "Api/1/{controller}/{action}/{id}",
    new { action = "Index", id = UrlParameter.Optional });

I have tried the following, defined above the general one, which doesn't work:

//search api
context.MapRoute(
    "searchJson",
    "api/1/{controller}.{action}",
    new { controller = "SearchController" });

How would I configure routing to enable the .format-style urls?

解决方案

context.MapRoute(
    "Api_default",
    "{area}/1/{controller}.{format}",
    new { action = "Index", id = UrlParameter.Optional });

is probably what you want. Then you could just return the different results based on the argument passed in.

Within the context of an Api Area, the SearchController would look like this:

public class SearchController : Controller
{
    public ActionResult Index(string format, SearchModel search)
    {
        var results = searchFacade.SearchStuff(search);

        if(format.Equals("xml"))
            return Xml(results); //using an XmlResult or whatever
        if(format.Equals("html"))
            return View(results);
        return Json(results, JsonRequestBehavior.AllowGet);
    }
}

这篇关于自定义ASP.NET MVC路由到服务与QUOT;以.json" "&的.xml QUOT;样式的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆