ASP.NET MVC - 提取 URL 参数

发布于 2024-10-17 14:46:23 字数 367 浏览 7 评论 0原文

我正在尝试提取 URL 的参数,如下所示。

/Administration/Customer/Edit/1

摘录: 1

/Administration/Product/Edit/18?allowed=true

摘录:< /strong> 18?allowed=true

/Administration/Product/Create?allowed=true

提取: ?allowed=true

有人可以帮忙吗?谢谢!

I'm trying to extract the parameters of my URL, something like this.

/Administration/Customer/Edit/1

extract: 1

/Administration/Product/Edit/18?allowed=true

extract: 18?allowed=true

/Administration/Product/Create?allowed=true

extract: ?allowed=true

Someone can help? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

凉城 2024-10-24 14:46:23

更新

RouteData.Values["id"] + Request.Url.Query

将匹配您的所有示例


尚不完全清楚您想要实现的目标。 MVC 通过模型绑定为您传递 URL 参数。

public class CustomerController : Controller {

  public ActionResult Edit(int id) {

    int customerId = id //the id in the URL

    return View();
  }

}


public class ProductController : Controller {

  public ActionResult Edit(int id, bool allowed) { 

    int productId = id; // the id in the URL
    bool isAllowed = allowed  // the ?allowed=true in the URL

    return View();
  }

}

在默认值之前将路由映射添加到 global.asax.cs 文件将处理 /administration/ 部分。或者您可能想研究 MVC 区域。

routes.MapRoute(
  "Admin", // Route name
  "Administration/{controller}/{action}/{id}", // URL with parameters
  new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults

如果您需要的是原始 URL 数据,那么您可以使用控制器操作中可用的各种 URL 和请求属性之一,

string url = Request.RawUrl;
string query= Request.Url.Query;
string isAllowed= Request.QueryString["allowed"];

听起来 Request.Url.PathAndQuery 可能就是您想要的。

如果您想访问原始发布数据,您可以使用

string isAllowed = Request.Params["allowed"];
string id = RouteData.Values["id"];

Update

RouteData.Values["id"] + Request.Url.Query

Will match all your examples


It is not entirely clear what you are trying to achieve. MVC passes URL parameters for you through model binding.

public class CustomerController : Controller {

  public ActionResult Edit(int id) {

    int customerId = id //the id in the URL

    return View();
  }

}


public class ProductController : Controller {

  public ActionResult Edit(int id, bool allowed) { 

    int productId = id; // the id in the URL
    bool isAllowed = allowed  // the ?allowed=true in the URL

    return View();
  }

}

Adding a route mapping to your global.asax.cs file before the default will handle the /administration/ part. Or you might want to look into MVC Areas.

routes.MapRoute(
  "Admin", // Route name
  "Administration/{controller}/{action}/{id}", // URL with parameters
  new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults

If it's the raw URL data you are after then you can use one of the various URL and Request properties available in your controller action

string url = Request.RawUrl;
string query= Request.Url.Query;
string isAllowed= Request.QueryString["allowed"];

It sounds like Request.Url.PathAndQuery could be what you want.

If you want access to the raw posted data you can use

string isAllowed = Request.Params["allowed"];
string id = RouteData.Values["id"];
不念旧人 2024-10-24 14:46:23
public ActionResult Index(int id,string value)

该函数从 URL 获取值
之后,您可以使用以下函数

Request.RawUrl - 返回当前页面的完整 URL

RouteData.Values - 返回URL 值集合

Request.Params - 返回名称值集合

public ActionResult Index(int id,string value)

This function get values form URL
After that you can use below function

Request.RawUrl - Return complete URL of Current page

RouteData.Values - Return Collection of Values of URL

Request.Params - Return Name Value Collections

君勿笑 2024-10-24 14:46:23

您可以在 ControllerContext.RoutValues 对象中以键值对的形式获取这些参数列表。

您可以将其存储在某个变量中,然后在逻辑中使用该变量。

You can get these parameter list in ControllerContext.RoutValues object as key-value pair.

You can store it in some variable and you make use of that variable in your logic.

冷︶言冷语的世界 2024-10-24 14:46:23

我写了这个方法:

    private string GetUrlParameter(HttpRequestBase request, string parName)
    {
        string result = string.Empty;

        var urlParameters = HttpUtility.ParseQueryString(request.Url.Query);
        if (urlParameters.AllKeys.Contains(parName))
        {
            result = urlParameters.Get(parName);
        }

        return result;
    }

我这样称呼它:

string fooBar = GetUrlParameter(Request, "FooBar");
if (!string.IsNullOrEmpty(fooBar))
{

}

I wrote this method:

    private string GetUrlParameter(HttpRequestBase request, string parName)
    {
        string result = string.Empty;

        var urlParameters = HttpUtility.ParseQueryString(request.Url.Query);
        if (urlParameters.AllKeys.Contains(parName))
        {
            result = urlParameters.Get(parName);
        }

        return result;
    }

And I call it like this:

string fooBar = GetUrlParameter(Request, "FooBar");
if (!string.IsNullOrEmpty(fooBar))
{

}
蝶舞 2024-10-24 14:46:23

为了获取参数的值,您可以使用RouteData

更多上下文就更好了。为什么首先需要“提取”它们?你应该有一个像这样的操作:

public ActionResult Edit(int id, bool allowed) {}

In order to get the values of your parameters, you can use RouteData.

More context would be nice. Why do you need to "extract" them in the first place? You should have an Action like:

public ActionResult Edit(int id, bool allowed) {}
贵在坚持 2024-10-24 14:46:23

我不熟悉 ASP.NET,但我想您可以使用 split 函数使用 / 作为分隔符将其拆分为数组,然后获取数组中的最后一个元素(通常是数组长度-1)来获取您想要的提取物。

好吧,这似乎并不适用于所有示例。

正则表达式怎么样?

.*(/|[a-zA-Z]+\?)(.*)

然后得到最后一个子表达式(.*),我相信它是.Net中的$+,我不确定

I'm not familiar with ASP.NET but I guess you could use a split function to split it in an array using the / as delimiter, then grab the last element in the array (usually the array length -1) to get the extract you want.

Ok this does not seem to work for all the examples.

What about a regex?

.*(/|[a-zA-Z]+\?)(.*)

then get that last subexpression (.*), I believe it's $+ in .Net, I'm not sure

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文