简体   繁体   English

如何在 ASP.NET Core MVC“待办事项”应用程序中提取 URL 的文件名并在视图中显示给用户?

[英]How do I extract the filename of a URL in a ASP.NET Core MVC "To Do" app and show it to the user in the View?

I have an ASP.NET Core MVC To Do app.我有一个 ASP.NET Core MVC To Do应用程序。 When I start the app, the app has a default URL of https://localhost but how do I get the filename from a URL like this one https://localhost/14-11-2020 .当我启动该应用程序时,该应用程序的默认 URL 为https://localhost但如何从这样的 URL 获取filename https://localhost/14-11-2020 I need to get the 14-11-2020 as parameter and display it in a .cshtml file between <time></time> tags as 14 / 11 / 2020 .我需要获取14-11-2020作为参数并将其显示在<time></time>标记之间的.cshtml文件中为14 / 11 / 2020 Each time the user changes the date, it needs to show the tasks from that date (since this is a "TODO" app).每次用户更改日期时,它都需要显示该日期的任务(因为这是一个“待办事项”应用程序)。 I have already setup the model with a database with DateTime attribute.我已经使用具有DateTime属性的数据库设置了模型。

My question is that: how do I extract the filename of a URL and show it to the user in the View?我的问题是:如何提取 URL 的文件名并在视图中显示给用户?

how do I extract the filename of a URL and show it to the user in the View?如何提取 URL 的文件名并在视图中显示给用户?

The AbsolutePath property contains the path information that the server uses to resolve requests for information in C#. AbsolutePath属性包含服务器用于解析 C# 信息请求的路径信息。 (More Details) (更多细节)

passing data to view from controller (more details) I am using ViewData here,传递数据以从控制器查看(更多详细信息)我在这里使用ViewData

ViewData is a ViewDataDictionary object accessed through string keys. ViewData 是一个通过字符串键访问的 ViewDataDictionary 对象。 String data can be stored and used directly without the need for a cast, but you must cast other ViewData object values to specific types when you extract them.字符串数据可以直接存储和使用而无需强制转换,但在提取其他 ViewData 对象值时必须将它们强制转换为特定类型。 You can use ViewData to pass data from controllers to views and within views, including partial views and layouts.您可以使用 ViewData 将数据从控制器传递到视图和视图内,包括局部视图和布局。

[HttpGet]
    public IActionResult Index()
    {
      Uri baseUri = new Uri("http://www.contoso.com/");
      Uri myUri = new Uri(baseUri, "catalog/shownew.htm?date=today");
      string name = Path.GetFileName(myUri.AbsolutePath);
      ViewData["FileName"] = name;
      return View();
    }

For the Path class use namespace using System.IO;对于 Path 类,使用System.IO使用命名空间 and inside view use,和内部视图使用,

<title>@ViewData["FileName"] - WebApplication</title>

You can parse the url params from the method on the controller您可以从控制器上的方法解析 url 参数

public class ToDoController : Controller
{
    [HttpGet]
    [Route("{date:DateTime}")]
    public IActionResult Index(DateTime date)
    {
        //// filter by date

        return View();
    }
}

In the above example a url of http://localhost:1234/todo/01-01-2020 would pass a date time of '01/01/2020 00:00:00' into the todo controller method在上面的例子中,http://localhost:1234/todo/01-01-2020 的 url 会将日期时间 '01/01/2020 00:00:00' 传递到 todo 控制器方法中

Get the date from RouteData and store it in ViewBag or ViewDate in controller, then access it the view:从 RouteData 获取日期并将其存储在控制器中的 ViewBag 或 ViewDate 中,然后在视图中访问它:

Controller:控制器:

public IActionResult Index()
{
    string date = RouteData.Values.Values.LastOrDefault().ToString();
    var mydate = date.Replace("-", "/");
    ViewBag.CurrentDate = mydate;
    return View();
}

View:看法:

<time>@ViewBag.CurrentDate</time>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM