ASP.NET MVC路由中的句点(点) [英] Period (dot) in ASP.NET MVC Route

查看:88
本文介绍了ASP.NET MVC路由中的句点(点)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿朋友!

我正在编写类似ASP.NET MVC应用程序的API.我希望用户决定如何通过调用MVC应用程序(具有诸如route之类的文件扩展名)来格式化响应数据.

例如:
http://base.url/controller/action.xml
应该执行某种操作并以XML格式返回数据

http://base.url/controller/action.json
应该执行与xml版本完全相同的操作,仅使用Json格式化响应数据.

我在Global.asax中添加了溃败:

Hey friends!

I''m writing an API like ASP.NET MVC application. I want users to decide how the response data is formatted by a call to the MVC app with an file extension like route.

For example :
http://base.url/controller/action.xml
Should perform some kind of action and return data in XML format

http://base.url/controller/action.json
Should perform the exact same action as the xml version, only the response data is formatted using Json.

I''ve added a rout in the Global.asax :

routes.MapRoute("ResponseExtension",
    "{controller}/{action}.{datatype}",
    new {
        controller = "Home",
        action = "Index",
        datatype = UrlParameter.Optional 
    });



为了允许URL中使用句点,我在<system.web>部分的web.config中添加了<add key="ClientValidationEnabled" value="true"/>行.我什至在网上找到了一篇文章,告诉我我大声喊叫删除web.config中的扩展名,就像这样:



To allow the period in the URL, i''ve added the line <add key="ClientValidationEnabled" value="true"/> to my web.config in the <system.web> section. I even found some article on the web which told me I shout remve the extension in the web.config like so :

<buildProviders>
    <remove extension=".xml"/>
    <remove extension=".json"/>
</buildProviders>



仍然,在som播放并尝试使其正常工作之后,仍然是相同的错误:
找不到资源
说明:HTTP404.您正在寻找的资源(或其依赖项之一)可能已被删除,名称已更改或暂时不可用.请查看以下URL,并确保其拼写正确.

现在最大的问题是:如何使它工作?
仅供参考:使用带有C#.NET 4.0的MVC 3

提前谢谢!
Eduard



Still, after som playing and trying to get it to work, still the same error :
The resource cannot be found
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Now the big question is : How do I get it to work?
FYI : Working with MVC 3 with C#.NET 4.0

Thanks in advance!
Eduard

推荐答案

我能够实现这样的目标.检查一下:

首先,我创建了一个控制器来进行测试:

I was able to achieve something like this. Check it out:

First, I created a controller for testing this:

public class RoutesController : Controller
    {
        //
        // GET: /Routes/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult About()
        {
            return View();
        }
    }



在现有的视图之一(除了下面的两个视图之外)中,我添加了以下内容:



In one of the existing views (apart from the 2 views below) I added this:

<%= Html.RouteLink("urls", "Custom") %>



然后在视图/路线"下创建一个索引和关于"视图,如下所示:



Then a Index and About view under Views/Routes as shown below:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Index
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>Index</h2>

    index.mvc

    <br />

    <%= Html.RouteLink("more","ResponseExtension", new { datatype = "json" }) %>

</asp:Content>

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    About
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>About</h2>

</asp:Content>



然后将以下内容添加到您的RegisterRoutes:



Then add the following to your RegisterRoutes:

routes.MapRoute("Custom", "routes/index.mvc", new { controller = "Routes", action = "Index" });

            routes.MapRoute("ResponseExtension",
                            "testpg/about.{datatype}",
                            new
                            {
                                controller = "Routes",
                                action = "About",
                                datatype = @"\[a-z]+"
                            });



它对我来说很好!试试看吧!

编辑-如果我将testpg/about.{datatype}更改为testpg/{action}.{datatype},虽然它没有用.我会在有空的时候尝试一下!



It worked fine for me! Give it a try!!

EDIT - If I changed testpg/about.{datatype} to testpg/{action}.{datatype} it didn''t work though. I will try that out too when I find some time!


这篇关于ASP.NET MVC路由中的句点(点)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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