Troubleshooting problem encountered with URL.Action routing

Hey everyone,

I'm working with a route/action in my controller that looks like this:

[RoutePrefix("widgets/download-functions")]

[Route("download/{publishedReportId}"), HttpGet]
public ActionResult Download(int publishedReportId)

Now, I'm trying to set up some routing in my JavaScript code using the appropriate id.

Here's what I have so far in my js code (but it's not working). Can you spot what I'm missing?

self.downloadFile = function (data) {
    console.log(data);
    console.log("@(Url.Action("Download", new { publishedReportId = 9999 }))");
    console.log("@(Url.Action("Download"))");
};

I'm still pretty new at this and learning as I go, so I'm sure it's something simple.

When I run this code, the console outputs the following:

Console output

https://i.sstatic.net/QfHWu.png

Thanks, jonpfl

Answer №1

When utilizing Url.Action, keep in mind that it doesn't optimize attribute routing. You will have to stick to the traditional method of routing, which involves using Action, Controller, area, and params to generate the URL through the extension method.

[RoutePrefix("widgets/download-functions")]
public class WidgetDownloadController : Controller

...

[Route("download/{publishedReportId}"), HttpGet]
public ActionResult Download(int publishedReportId)

The Url.Action would look something like this:

console.log("@(Url.Action("Download","WidgetDownload", new { publishedReportId = 9999 }))");

Keep in mind that this approach is applicable only to scripts parsed by the razor engine, specifically those directly coded within the view.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for keeping the mobile menu active and responsive

Struggling to implement this menu on my website, I encountered a problem: how do I keep the menu active? I envision the menu bar changing when users navigate to the Home page, appearing like this: https://i.sstatic.net/z2fbX.png I attempted to use the cl ...

Error: Trying to access the parent node of an undefined property

After incorporating this script into my webpage for the newsletter sign-up process, I aimed to reveal customized content post user subscription. <script> function insertAfter(referenceNode, newNode) { referenceNode.parentNode.insertBefore(newNode ...

The fragment shader must not declare any varyings with the same name but different type, or statically used varyings without being declared in the vertex shader. An example of this is

I'm struggling with shaders because I want the fog to be reflected in the water using Three.js sky_sun_shader. I added the following code snippet to the fragment Shader: THREE.ShaderChunk[ "fog_pars_fragment" ], THREE.ShaderChunk ["fog_fragment" ], ...

JavaScript Class Reference Error

Questioning the mysterious reference error in the JS class from MDN page. The structure of the Bad class's constructor leaves me baffled – is it because the empty constructor calls super() as a default? class Base {} class Good extends Base {} cla ...

Iterate through the list of objects and display duplicates only once

var fixtures = [ { "matchday": 1, "homeTeamName": "Arsenal FC", "awayTeamName": "Leicester City FC" }, { "matchday": 1, "homeTeamName": "AFC Bournemouth", ...

What is the optimal approach for importing node modules using var or const?

When it comes to requiring node modules like express or bodyParser, the commonly used keyword to create a variable and assign the module is var. However, is it possible to use const to declare such modules instead? In other words, instead of the following: ...

Exploring the World of Images with Javascript

I'm currently working on creating a slideshow using HTML and JavaScript. My goal is to have the image change each time I press a button that I've established (see code below). After reviewing my code multiple times, I still can't figure out ...

Utilize time in submitting a form

Is it possible to schedule a form submission at a specific time, such as 12:00? I have already created a countdown timer and now would like the form to be submitted automatically at a certain time. <html> <head> </head> <body> ...

The Fetch API's Post functionality is currently experiencing issues

fetch('http://localhost:9000/api/app/contact', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ email: this.state.email, contactNumber: ...

The dropdown menu is dynamically populated based on the selection from another dropdown menu, with options retrieved from

Apologies if this has been addressed previously, but the existing solutions do not seem to work for me. I have 5 dropdowns: Brand, Model, Color, Engine No., and Chassis No. My query pertains to how I can link the dropdown options for Model based on the sel ...

Establish indexes for each column in MySQL database

Currently, I am in the process of developing an application in C# that has the capability to dynamically generate a table based on a user input query. The specific syntax I am utilizing for this task is as follows: CREATE TABLE <table name> (<use ...

Alert: The `className` property did not align and there is an absence of the window within the Next 13 App Router

I've been struggling to fix an error in the Next.js 13 App Router. Warning: Prop `className` did not match. Server: "styled__HeadLink-sc-6589f63-0 fIOFTm" Client: "styled__HeadLink-sc-b76127ed-0 jzAIeh" I've spent a lot of ...

inquiry on php function properties

I have a PHP file containing two functions. <?php function first () { //in real case more conditions echo "first"; return true; } function second () { //in real case more conditions echo "second"; return true; } first(); second(); ?> And in an ...

Erase Photo from Server by Simply Clicking on the Remove Button NodeJS (And Removing the Image Title from the Database)

I have a button that successfully deletes an image name from a mySQL table. However, I also want it to delete the actual image from the server. Below is the code snippet from my index.js: document.querySelector('table tbody').addEventListener(&a ...

Canceling text as soon as the search input is entered in Vue.js

When I use the search input box, the last text I typed in gets cancelled. Can anyone help me with this issue? <input class="navbar-searchbar__text-field" type="search" :value="searchQuery" name=" ...

Image not loading after ajax request

Hello, I'm new to this platform and have encountered an issue that I can't seem to resolve on my own... Currently, I have a web application running on Zend with MVC. I am making an Ajax call using jQuery on the client side, which returns data in ...

Assign the input from the text box to a variable

How can I incorporate a value from a textbox into this code? Specifically, the subjectId is needed var body = @"{ ""requestId"": ""requestId"", ""subjectId"&quo ...

Encountering undefined outcomes when iterating over a JSON Array

After receiving a JSON String in a response variable through an AJAX request, I need to parse it: The JSON data { "TheArray":[ { "AlmostThere": { "whatWeAreLookingFor":"Hello" } }, { "Almos ...

using a post Response to trigger a new Post request

I've recently begun working on creating an API using nodeJS for the back-end of my front-end web application with Bootstrap. Although I just started learning front-end development yesterday, I've managed to fix most of the bugs. The only issue no ...

The JQuery ajax post function is typically called towards the conclusion of a JavaScript script

I am struggling with validating whether a username is already taken. I have been attempting to determine if the username exists by utilizing the "post" method in jQuery. However, every time I execute this function, the script seems to skip to the end of th ...