与ASP.net和C#路线的定义 [英] Definition of routes with ASP.net and C #

查看:120
本文介绍了与ASP.net和C#路线的定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有与ASP.net和C#路由的定义的问题,使用GET动词。
我得到这个URL,

  http://123.45.67.89:39051/dev/point/save?name=125.25

我得到这个URL,其​​中据我想要搜索的数据变化。 dev和保存参数不变。点的数量是大的,就需要使这可能需要用字符和数字的任何值。
修改文件WebApiConfig,尝试它可以采取任何价值,没有工作。

例如:

point12
point23
point24
MzaB342
Pozo123
MzaE258

WebApiConfig


  config.MapHttpAttributeRoutes();
           config.Routes.MapHttpRoute(
           名称:DefaultApi
           routeTemplate:开发/ {}分/保存,
           默认:新{点= RouteParameter.Optional}
           限制:新{点= @^ [A-Z] + $}        );


我怎样才能做到这一点?

这是正确的,我已经把^ [A-ZA-Z0-9] + $采取古兰经的数字,但如果问题仍然存在,联系电话发送查询要求

  http://123.45.67.89:39051/dev/values​​/save?name=125.25

正常工作。
如果我想咨询:

  http://123.45.67.89:39051/dev/point12/save?name=125.25

我得到一个404错误,因为没有路线point12,这些都是在数据库中的名称,并有大量的,不可能为每个人的路线,必须将它们重定向到一个,然后脱code这条路线,并赋值。
或者我在 http://123.45.67.89:39051/dev/xxyyzz121/save 让把它正确,我从URL,这让我单独控制的信息每个点的要求,使用数据绑定或解析查询。
我不能改变我得到的URL的方式,来自其他系统。

尝试测试解决方案把路径,当我进入类,在ActionResult的错误,你需要加载System.Web.Mvc,因为我使用的WebAPI没有MVC,无法找到它,如果它给我的WebAPI其他类添加的错误。
由于可以做定义缺省路由。

Web.Api.Config code:

 命名空间WebApp_dev
{
     公共静态类WebApiConfig
     {
         公共静态无效的注册(HttpConfiguration配置)
         {
             //配置API和Web服务             //网页API路线
             config.MapHttpAttributeRoutes();             config.Routes.MapHttpRoute(
                 名称:DefaultApi
                 routeTemplate开发/ {}控制器/(编号),
                 默认:新{控制器=值,ID = RouteParameter.Optional,},
                 限制:新{控制器= @^ [A-ZA-Z0-9] + $}
             );
         }
     }
}

Values​​Controller.cs code:

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Net;
使用System.Net.Http;
使用System.Web.Http;命名空间WebApp_dev.Controllers
{
     公共类Values​​Controller:ApiController
     {
         //获取API /值
         公共IEnumerable的<串GT;获得()
         {
             返回新的字符串[] {值1,值2};
         }         //获取API /价值/ 5
         公共字符串GET(INT ID)
         {
             回到价值;
         }         // POST API /值
         公共无效后([FromBody]字符串值)
         {
         }         // PUT API /价值/ 5
         公共无效认沽(INT ID,[FromBody]字符串值)
         {
         }         //删除API /价值/ 5
         公共无效删除(INT ID)
         {
         }
     }
}

没有,途径的名称更改,他们的价值观,可以muchs之间达到网址是:

  /dev/nombre1/save?name1=123.56

 的/ dev / point123 /保存?name12 = 12

 的/ dev / pozo12 /保存?值1 = 13

 的/ dev / mbz134 /保存?costo2 = 13

 的/ dev / patag235 /保存?name8 = 13

什么改变是 NAME1,point123,pozo12,mbz134 等的量是大的,被存储在数据库中,并根据该到来时,它是完成了搜索和发送的响应。
该URL的的/ dev 并开始结束的 /保存?它保持不变,什么是后保存?名称1,name12 ,值1,costo2,name8 的)是改变参数引用也:名称1,point123,pozo12,mbz134 的等,这也改变

功能正常,反应非常好。

还有一个问题,我需要单独为我访问数据的变量点的名称,例如:

  http://123.45.67.89:8090/dev/nombre1/save?name1=12.56&di2=1&an1=5

存储在一个变量( nombre_pto = ),输入的值: 名1

  http://123.45.67.89:8090/dev/patag235/save?name1=3.56&name2=2.36&val4=5

存储在一个变量( nombre_pto = ),输入的值: patag235

  http://123.45.67.89:8090/dev/mza341/save?name1=123.56&pos2=23.36

存储在一个变量( nombre_pto = ),输入的值: mza341

这将需要根据参数来搜索数据库和过滤的信息。

从已经非常感谢你

我可以解决。

 字符串url_completa = Request.RequestUri.AbsolutePath;
  //我正在寻找埃尔农布雷德尔PUNTO德medicion -----------
  INT _indexPto = url_completa.IndexOf(的/ dev /);
  url_completa = url_completa.Remove(0,(_indexPto + 5))


解决方案

根据问题的描述中做到这一点的变化进行更新。

要处理此类URL,你应该:


  1. 在WebApiConfig注册通配符路线:

      config.Routes.MapHttpRoute(
          名称:DefaultApi
          routeTemplate:开发/ {}点/保存,
          默认:新{控制器=值,行动=保存},
          限制:新{点= @^ [A-ZA-Z0-9] + $}
      );


  2. 写您的控制器来处理这条路线:

     公共类Values​​Controller:ApiController
    {
         [HTTPGET]
         公共字符串保存(串点)
         {
             //在点变量,你会得到之间的/ dev部分和/保存
             //为/dev/point/save?name=125.25网址这将是点         //得到遵循参数?大关网址
             VAR queryParams = GetQueryParams(请求);         //通过所有这些循环
             的foreach(在queryParams VAR对)
             {
                字符串paramName配置= pair.Key; //为/dev/point/save?name=125.25将名
                字符串paramvalue = queryParams [pair.Key] //为/dev/point/save?name=125.25将是125.25
             }         返回一些价值;
          }     私人字典<字符串,字符串> GetQueryParams(HTT prequestMessage要求)
         {
             返回request.GetQueryNameValuePairs()
                          .ToDictionary(KV => kv.Key,KV => kv.Value,
                               StringComparer.OrdinalIgnoreCase);
         }
    }


其他URL也适合这个控制器,因此这个动作。

。希望最后却是你问什么。

I have a problem with the definition of routes with ASP.net and C #, using the GET verb. I get this URL,

http://123.45.67.89:39051/dev/point/save?name=125.25

I get this URL, where point changes according to where I want to search the data. The dev and save parameters are constant. The number of points is large, would need to make this could take any value with characters and numbers. Modify the file WebApiConfig, trying it can take any value and did not work.

For example:

point12 point23 point24 MzaB342 Pozo123 MzaE258

WebApiConfig

           config.MapHttpAttributeRoutes(); 
           config.Routes.MapHttpRoute(
           name: "DefaultApi",
           routeTemplate: "dev/{points}/save",           
           defaults: new { points= RouteParameter.Optional }
           constraints: new { points= @"^[a-z]+$" }

        );

How can I do this?

It is correct that I have to put "^ [a-zA-Z0-9] + $" to take qur numbers, but if problem persists tel sent a query a requirement

http://123.45.67.89:39051/dev/values/save?name=125.25

Works fine. If I want to consult:

http://123.45.67.89:39051/dev/point12/save?name=125.25

I get a 404 error, because there is no route point12, these are name within a database, and there are a large amount, could not generate a route for each of them, have to redirect them to one and then to decode this route and assign the value. Or what I get after http://123.45.67.89:39051/dev/xxyyzz121/save take it correctly, I separate the information from the URL, which allows me to control the request for each point, using data binding or Parse Query. I can not change the way I get the URL, that comes from another system.

Try to test the solution to put the path and when I enter the class, have an error in ActionResult, you need to load System.Web.Mvc, as I am using WebAPI without MVC, can not find it, if it gives me errors added in WebAPI other classes. As could be done to define default routes.

Web.Api.Config Code:

namespace WebApp_dev
{
     public static class WebApiConfig
     {
         public static void Register (HttpConfiguration config)
         {
             // Configuration API and Web services

             // Web API routes
             config.MapHttpAttributeRoutes ();

             config.Routes.MapHttpRoute (
                 name: "DefaultApi"
                 routeTemplate "dev / {controller} / {id}",
                 defaults: new {controller = "values", id = RouteParameter.Optional,},
                 constraints: new {controller = @ "^ [a-zA-Z0-9] + $"}
             );
         }
     }
}

ValuesController.cs Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace WebApp_dev.Controllers
{
     public class ValuesController: ApiController
     {
         // GET api / values
         public IEnumerable <string> Get ()
         {
             return new string [] {"value1", "value2"};
         }

         // GET api / values ​​/ 5
         public string Get (int id)
         {
             return "value";
         }

         // POST api / values
         public void Post ([FromBody] string value)
         {
         }

         // PUT api / values ​​/ 5
         public void Put (int id, [FromBody] string value)
         {
         }

         // DELETE api / values ​​/ 5
         public void Delete (int id)
         {
         }
     }
}

No, the name of the route changes, they are values​​, the URL that can reach between muchs are:

/dev/nombre1/save?name1=123.56

or

/dev/point123/save?name12=12

or

/dev/pozo12/save?value1=13

or

/dev/mbz134/save?costo2=13

or

/dev/patag235/save?name8=13

What changes is name1, point123, pozo12, mbz134 etc. The amount is large, are stored in a database, and according to that comes, it is the search is done and the response sent. The beginning of the URL /dev and end /save? It stays constant , what is after the save? (name1, name12, value1, costo2, name8) are the parameters that change and references also: name1, point123, pozo12, mbz134 etc, which also change.

Function properly, very good response.

One more question I need to separate a variable point name for which I access the data, for example:

http://123.45.67.89:8090/dev/nombre1/save?name1=12.56&di2=1&an1=5

stored in a variable (nombre_pto =), the value entered: name1

http://123.45.67.89:8090/dev/patag235/save?name1=3.56&name2=2.36&val4=5

stored in a variable (nombre_pto =), the value entered: patag235

http://123.45.67.89:8090/dev/mza341/save?name1=123.56&pos2=23.36

stored in a variable (nombre_pto =), the value entered: mza341

This will need to search the database and filter the information according to the parameters.

From already thank you very much

I could solve.

  string url_completa = Request.RequestUri.AbsolutePath;
  // Busco el nombre del punto de medicion-----------
  int _indexPto = url_completa.IndexOf("/dev/");
  url_completa=url_completa.Remove(0, (_indexPto + 5))

解决方案

UPDATED according to changes that were done in question description.

To handle such urls you should:

  1. Register wildcard route in WebApiConfig:

      config.Routes.MapHttpRoute(
          name: "DefaultApi",
          routeTemplate: "dev/{point}/save",
          defaults: new { controller = "Values", action = "Save" },
          constraints: new { point = @"^[a-zA-Z0-9]+$" }
      );
    

  2. Write your controller to process this route:

    public class ValuesController : ApiController
    {
         [HttpGet]
         public string Save(string point)
         {
             // in point variable you will get part between /dev and /save
             // for /dev/point/save?name=125.25 url it will be "point"
    
             // get parameters that follow by ? mark in url
             var queryParams = GetQueryParams(Request);
    
             // loop through all of them
             foreach (var pair in queryParams)
             {
                string paramName = pair.Key;  // for /dev/point/save?name=125.25 will be "name"
                string paramvalue = queryParams[pair.Key]; // for /dev/point/save?name=125.25 will be 125.25
             }
    
             return "some value";
          }
    
         private Dictionary<string, string> GetQueryParams(HttpRequestMessage request)
         {
             return request.GetQueryNameValuePairs()
                          .ToDictionary(kv => kv.Key, kv => kv.Value,
                               StringComparer.OrdinalIgnoreCase);
         }
    }
    

Other urls are also fit this controller and this action.

Hope that finally it is what you were asking for.

这篇关于与ASP.net和C#路线的定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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