【发布时间】:2019-03-15 03:10:05
【问题描述】:
在 ASP.NET MVC 中,可以这样定义路由:
routes.MapRoute("myroute",
"myroute/{country}/{name}-{type}",
new { controller = "MyController", action = "Get" });
这会将它直接解析为一个对象:
public class MyController : Controller
{
public HttpResponseMessage Get([FromRoute] MyViewModel model)
{
//TODO do stuff with model.
}
}
这是我的视图模型:
public class MyViewModel
{
public string Name { get; set; }
public string Type{ get; set; }
}
我的问题是,我可以在一个简单的控制台应用程序中进行相同的解析吗?
class Program
{
static void Main(string[] args)
{
string route = "myroute/{country}/{name}-{type}";
string input = "myroute/Denmark/MyName-MyType";
//TODO Parse input to MyViewModel with route
MyViewModel result;
}
}
public class MyViewModel
{
public string Name { get; set; }
public string Type { get; set; }
}
必须有某种方法可以做到这一点,因为它可以用于 ASP.NET MVC 路由。
【问题讨论】:
-
肯定有办法做到这一点,写一些代码来解析你的值并用
PropertyInfo设置它们,试一试,有很多东西,从正则表达式开始或拆分 -
不知道有没有内置方式。我很想将路由转换为正则表达式(您可以自动执行此操作),然后您可以针对生成的正则表达式测试“url”。如果您使用
{}中的名称来命名您的正则表达式组,您还可以轻松提取数据。 -
不打算使用拆分,这样就无法将路由更改为其他内容。
-
一些正则表达式可能会起作用,只是不确定如何解析为一个对象。并且当它可以用于 asp.net mvc rounting 时,它必须可以用一个简单的字符串来做同样的事情。
-
让我们看看是否有人可以做出更好的解决方案,但这是一个开始:)
标签: c# asp.net-mvc asp.net-core .net-core asp.net-mvc-routing