私は ASP.net を初めて使用し、最近 asp.net から asp.net MVC プロジェクトに変換しています。asp.net MVC Url.Action で基本的な質問が 1 つあります。
ActionResult
コントローラーに2 つのパラメーターを持つ 1 つのメソッドとQuestionId, Response
、次のようなルーティング構成があります。
public class QuestionController : Controller
{
public ActionResult Answer(int QuestionId, string Response)
{
..do something.....
}
}
としてマッピングされたルーティング構成
routes.MapRoute(
name: "QuestionAnswer",
url: "{controller}/{action}/{QuestionId}/{Response}",
defaults: new { controller ="Question", action = "Answer", QuestionId = 0, Response = string.Empty}
);
以下の構文から アクション Link in html page を呼び出す
@Html.ActionLink("Answer is 1", "Answer", "Question", null, null, null, new RouteValueDictionary(new {QuestionId = 10, Response = "1" }), null)
/Question/Answer?QuestionID=10&Response=1 のように、この URL はルーティング定義に一致し、適切に Action を呼び出します。すべて順調。
しかし、私の実際の要件は、これらのパラメーター名を公開しないことです。/Question/Answer/10/1 のようになるはずです
String.Format
私のサイトはデフォルトの Web サイトではなく、仮想ディレクトリ内に配置されている可能性があるため、このリンクを の助けを借りて手動でフォーマットしたくありません。