简体   繁体   English

ASP.NET Core 2.0 URL

[英]ASP.NET Core 2.0 URLs

I'm developing a large ASP.NET Core 2 web application but I still get confused with URLs.我正在开发一个大型 ASP.NET Core 2 Web 应用程序,但我仍然对 URL 感到困惑。

When fist learning, I thought URL's took on the name of the View, but discovered they come from the Controller method.刚开始学习时,我以为 URL 取的是 View 的名称,但发现它们来自 Controller 方法。

Here is an example to convey my issue:这是一个传达我的问题的示例:

I have a controller method named Daysheet , which returns a view and model with the same name.我有一个名为Daysheet的控制器方法,它返回一个具有相同名称的视图和模型。

In the Daysheet view, I call various controller methods from Javascript to perform specific actions.在 Daysheet 视图中,我从 Javascript 调用各种控制器方法来执行特定操作。 One of them is called AssignStaff which takes two integer parameters.其中之一称为AssignStaff ,它采用两个整数参数。

In the AssignStaff method I again return the Daysheet view with model, but now my URL is "AssignStaff"!在 AssignStaff 方法中,我再次返回带有模型的 Daysheet 视图,但现在我的 URL 是“AssignStaff”!

I can't just do a redirect because the whole Daysheet model is not being passed to the AssignStaff method.我不能只进行重定向,因为整个 Daysheet 模型没有传递给 AssignStaff 方法。

I have many situations like this where after calling an action, I end up with another URL that I don't want.我有很多这样的情况,在调用操作后,我最终得到另一个我不想要的 URL。

UPDATE/EDIT更新/编辑

Thanks for assistance and apologies if my explanation is confusing.如果我的解释令人困惑,感谢您的帮助和道歉。 I simply have a view called Daysheet that uses a model.我只是有一个名为Daysheet的视图,它使用了一个模型。 I want to call various controller methods to perform various actions, but I want to stay on the "Daysheet" view/URL.我想调用各种控制器方法来执行各种操作,但我想留在“Daysheet”视图/URL 上。

As mentioned, I can't just redirect because in the action method I no longer have the whole model from the Daysheet view.如前所述,我不能只是重定向,因为在操作方法中,我不再拥有Daysheet视图中的整个模型。 Also, if I redirect I can't pass the whole model because that causes an error saying the header is too long.另外,如果我重定向,我将无法传递整个模型,因为这会导致错误,指出标题太长。 I think my only choice may be to use ajax for the actions so that the URL doesn't change.我认为我唯一的选择可能是使用 ajax 进行操作,这样 URL 就不会改变。

When you just do Return View("") name in a Controller Action, the URL will be the name of the Action you are using.当您只在控制器操作中执行 Return View("") name 时,URL 将是您正在使用的操作的名称。

If you want to redirect to some specific Action, that will help to make sure the Url matches to where you are.如果您想重定向到某个特定的操作,这将有助于确保 Url 与您所在的位置匹配。 You might want to read more about it here .您可能想在此处阅读更多相关信息。

To do so, use:为此,请使用:

RedirectToAction()

The URLs your application responds to are called "routes", and they are either created by convention or explicitly.您的应用程序响应的 URL 称为“路由”,它们要么是按约定创建的,要么是明确创建的。 The default is by convention, of course, which is a URL in the form of /{controller=Home}/{action=Index} .当然,默认是按照惯例,它是/{controller=Home}/{action=Index}形式的 URL。 Index is the default action if that portion of the route is left off, so a request to /foo will by convention map to FooController.Index .如果路由的那部分被保留, Index是默认操作,因此对/foo的请求将按照约定映射到FooController.Index HomeController is the default controller, so an empty path (eg http://sample.com ) will by convention invoke HomeController.Index . HomeController是默认控制器,因此按照惯例,空路径(例如http://sample.com )将调用HomeController.Index

Razor Pages have their own conventions. Razor Pages 有自己的约定。 These do somewhat follow the file system, but exclude the Pages part of the path.这些确实遵循文件系统,但不包括路径的Pages部分。 So a Razor Page like Pages/Foo/MyRazorPage.cshtml , will load up under /Foo/MyRazorPage .因此,像Pages/Foo/MyRazorPage.cshtml这样的 Razor 页面将在/Foo/MyRazorPage/Foo/MyRazorPage

There there is the Route attribute, which allows you to specify a totally custom route.Route属性,它允许您指定一个完全自定义的路由。 This attribute can be applied to a controller class and individual actions in the class.此属性可以应用于控制器类和类中的单个操作。 For example:例如:

[Route("foo")]
public class MyAwesomeController
{
    [Route("bar")]
    public IActionResult MyAwesomeAction()
    {
        return View();
    }
}

With that, a request to /foo/bar will actually invoke MyAwesomeController.MyAwesomeAction .这样,对/foo/bar的请求实际上会调用MyAwesomeController.MyAwesomeAction

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

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